Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery css method with 3 parameters

Tags:

jquery

I came across a line of code using jQuery css method as below

var $container = $('#mycontainer');  <br>
var w = $.css($container[0], "width", true);<br>

jQuery documentation on css method was checked and didn't mention any variant with 3 parameters. I test the code in jsbin and it worked fine - return value is a number. if the last parameter was changed to false, the return is a string like 200px.

could anyone highlight me on this special syntax on method css?

Is it something deprecated ?

like image 430
david2028 Avatar asked Mar 06 '26 15:03

david2028


1 Answers

It's just the static version of css, not the jQuery element-specific instance version, so it also needs the object to apply it to.

The normal version would look like this for the code shown:

   $container.css("width");

So you can see the static version just moves the target element into a first parameter. Otherwise how would it know what to apply the style too :)

true is not really a valid value for "width", so it looks like the static version uses that as a flag to say whether to return the value as a string (with the suffix px) or as a value.

like image 124
Gone Coding Avatar answered Mar 08 '26 20:03

Gone Coding