Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using shorthand assignment in jQuery for element attributes

To double the width of the img, i can do this in jQuery:

<img src='blah.jpg' id='pic' />

$('#pic').height($(this).height()*2);

that works fine, but i really like to use shorthand assignments like:

var count = 5;
count *= 2; // to get 10.

Since element.height returns the height function in jQuery, i can't use shorthand assignments. Is there no way to do shorthand assignments in jQuery for element attributes?

like image 891
Ray Cheng Avatar asked Jun 04 '12 05:06

Ray Cheng


1 Answers

You can modify the attributes such as height without using jQuery

document.getElementById("pic").height *= 2;

Or if you want to select the element with jQuery you can use this snippet. It selects the element and then accesses it directly using the indexer.

$("#pic")[0].height *= 2;
like image 87
John Avatar answered Nov 02 '22 01:11

John