4

Is it possible to pass function as a value to all the css property? Refer the code here:

    <script>
  $("div").click(function() {
    $(this).css({
      width: function(index, value) {
        return parseFloat(value) * 1.2;
      },
      height: function(index, value) {
        return parseFloat(value) * 1.2;
      }

    });
  });
</script>

I really dont have any clue why the above code is working.Can anyone explain advantages as well as disadvantages of passing function to properties?Also is this method of passing func tion applicable to all other css properties and non-css properties? plz help clarify in detail

2

2 Answers 2

1

According to the api document from jQuery, passing function instead of directly value as css value is possible. It is added in version added: 1.4. From the document :

.css( propertyName, function(index, value) )
propertyName
Type: String
A CSS property name.

function(index, value)
Type: Function()
A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old value as arguments.

The basic idea is, when you use function as value. It will call the function with the element index and current value as argument for dynamic computation or set under some conditions. Then after compute or condition, return values for it to set the target "propertyName"

For example: You can think there is a larger button in your web site and you would like the user double size the current font size when clicked the button. You can make use of api to achieve that. In addition, I don't think you can pass multiple function to css method as value.

Working example on : http://jsfiddle.net/BUyp9/1/

HTML

<input type="button" value="larger size" id="enlargeBtn" />
<div id="content">
 Hello world.    
</div>

JavaScript

$("#enlargeBtn").on('click', function(){
    $("#content").css('font-size', function(index, value) {
        var size = parseInt(value, 10);
        return size*2;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

It lets you set a new value based on the old value (which is what the value parameter contains), or based on the details of each particular element of the jQuery selection (which is what index tells you, as well as this).

So for example, if you wanted to make the background color of each <div> darker regardless of starting color, you could write code to take the old value and decrease the lightness (not trivial but not super-difficult either). The very example you posted is another good one: it's making the elements 20% bigger (well it's making the width and height 20% bigger; I guess that means the area gets 44% bigger).

Functions can be used as values in the simple case of the .css() method:

$('something').css("background-color", function(index, value) { ... });

Or, as in your example, functions can be the values of properties in an object passed to the method:

$('something').css({
  "background-color": function(index, value) { ... },
  "width": function(index, value) { ... },
  // ...
});

You can mix and match:

$('something').css({
  "height": "100px",
  "z-index": function(index, value) { ... }
});

6 Comments

but the function is meant to be passed to css() method not the property
@Maizere ?? the function is the value in the object, so jQuery will call the function when it's acting on the .css() update request. It's not "passed" to the property; it's the value of the property.
is this method applicable to non-css property as well?
can we pass multiple function to css method?Really getting confused
Your sample code is passing an object to the .css() method. That object has named properties corresponding to the CSS properties to be set. Those properties can have values that are either the actual values for jQuery to assign to the CSS properties, or they can be functions. If jQuery sees that a property value is a function, it calls the function and uses its return value as the actual CSS property value. It's that simple.
|

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.