Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: What is a "Value Callback"?
    primarykey
    data
    text
    <p>I'm working my way through "Learning jQuery" (Third Edition).</p> <p>In Chapter 4: "Manipulating the DOM" there is a section explaining something called the "<strong><em>Value Callback</em></strong>". This is a new one for me.</p> <p>The author explains this via an example of list of links wherein the ID's of each <em>must be unique</em>.</p> <p>From the book:</p> <blockquote> <p>"A value callback is simply a function that is supplied instead of the value for an argument. This function is then invoked once per element in the matched set. Whatever data is returned from the function is used as the new value for the attribute. For example, we can use this technique to generate a different id value for each element, as follows:"</p> <p>Chaffer, Jonathan (2011-09-23). Learning jQuery, Third Edition (p. 116). Packt Publishing. Kindle Edition. </p> </blockquote> <pre><code>jQuery(document).ready(function($){ // get all external links $('div.chapter a').attr({ rel:'external', title:'Learn more at Wikipedia', id: function ( index, oldValue ) { return 'wikilink-' + index; } }); }) </code></pre> <p>Works like a charm, but <strong><em>the mechanics</em></strong> of the id: property escape me.</p> <ol> <li><strong>How</strong> does parameter 1 (index) <strong><em>know</em></strong> to be an integer?</li> <li>How does the function know to <strong><em>increment</em></strong> index?</li> <li>How does the second parameter (oldValue) know to hold the old value of the property (before modification)?</li> <li>Is this a jQuery construct? A JSON thing? It's cool. it works, but ...what the heck <strong><em>is</em></strong> this "value callback" thing made of?</li> </ol> <p>Please advise</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload