1

How to modify an image's attr use jquery? I want set the image's height as the window.height

http://jsfiddle.net/wyCqT/

<script type="text/livescript"> $(document).ready(function(){ var height = $(window).height(); $('img').attr('height',height); }); </script> <img src="http://farm7.static.flickr.com/6124/6022097678_4477a09976_o.jpg" /> 
1
  • Your fiddle works perfectly for me! Commented Sep 16, 2011 at 9:24

2 Answers 2

2

I think you could use:

$('img').attr('height', $(window).height()); 

JS FIddle demo

Or you could use prop() in place of attr():

$('img').prop('height', $(window).height()); 

JS Fiddle demo

But both of these are variations on what you've already written, the problem you're having is that you've used type="text/livescript" in your style tag, if you amend that to: style="text/javascript" it works.

References:

Sign up to request clarification or add additional context in comments.

Comments

0

You should not change the height attribute, you should change the height css.

And for window height in particular you can just do:

html, body { height: 100%; } 

Then add:

style="height: 100%;" 

To your image and you don't need to mess with jQuery at all.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.