$('.column').each(function(){ // *this* IS the DOM element! // There's no need to convert it into a jQuery object var data = commentData( this ); var columnWidth = data.width; }); |
$(this)
is a jQuery object – commentData
requires a DOM element reference. Here, try this:
$('.column').each(function(){ var data = commentData( $(this)[0] ); // [0] references the DOM element var columnWidth = data.width; }); |
$('.column').each(function(){ var data = commentData($(this)); var columnWidth = data.width; }); |
I’m probably just being thick but if each column had an ID then I could do this….
$('.column').each(function(){ var data = commentData(document.getElementById($(this).attr('ID'))); var columnWidth = data.width; }); |
@Jon, why would you want to supply it with a selector? The data is tied to the parent (commentNode.parentNode
). This technique does not require the element to have an ID. The only reason I’ve given the element an ID in the example was to illustrate how you might extract that data via JavaScript.
@Evan, Sometimes, you may need to have a large amount of data existing within the document so you can use it when the user interacts with the page – this is preferred over Ajax when you have a small dataset and when interactivity is likely. There are many different applications. Read:
– http://loadaveragezero.com/app/drx/Data_Formats/Metadata
– http://www.1729.com/blog/HtmlAnnotations.html
– http://www.mail-archive.com/[email protected]/msg06489.html
Also have a look at jQuery’s “metadata” plugin (created by John Resig himself) to see an alternative implemenation.
]]>Would this be easy enough?
]]>