Using shorthand in jQuery for element attributes

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

 <img src='blah.jpg' id='pic' /> $('#pic').height($(this).height()*2); 

which works great, but I really enjoy using shorthand jobs, for example:

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

Since element.height returns a height function in jQuery, I cannot use shorthand jobs. Is it not possible to perform short jobs in jQuery for element attributes?

+6
source share
3 answers

You can change attributes like height without using jQuery

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

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

 $("#pic")[0].height *= 2; 
+13
source
 $('#pic').height($('#pic').height() * 2); 

or

  $('#pic')[0].height *= 2; 
0
source

Answers a question on how to do this in jQuery

 $('#pic').height(function( i, ht){ return ht *2; }); 

See the added API function in 1.4.1 http://api.jquery.com/height/

0
source

Source: https://habr.com/ru/post/917340/


All Articles