Often when calling the attr action in jQuery you use the attr(someKey, someValue) action, which generally is used to set attributes to some value. While we typically expect this value to be static, the attr action allows us to pass in a function to determine the value. Typically the function will need to be passed two parameters. The first being the index of the current element and its corresponding value. Where whatever the function returns for a value is what the attr action sets for the elements new value.
This type of dynamic processing took me a little while to get used to, but once you get it the possibilities are great. You can run this on a whole stack of commands like val, html, and others. This can make it very easy to change your DOM elements based on changing criteria rather than relying on a static template. Rarely does anything static last in the internet these days...
No comments:
Post a Comment