3

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 ?

1
  • +1 for pointing out that subtle difference. Might come in very handy getting the raw string version (which the element version does not). Commented May 30, 2014 at 8:51

2 Answers 2

3

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.

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

2 Comments

thanks! just more question : in this case, what's the meaning of the second boolean parameter (true)?
true = Return as number (removes the px), false = return as original text.
0

Here is a link to the source code of the jQuery.css method. I didn't look into much of the details, but it seems, that there is the extra parameter, which changes the output format in combination with a cssHook defined specially for the width property.

https://github.com/jquery/jquery/blob/master/src/css.js#L302-L334

1 Comment

Argh... No... He pulled back the curtain... IT'S NOT MAGIC AFTER ALL! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.