Just jQuery The Core UI - Data |
Written by Ian Elliot | ||||
Sunday, 15 October 2023 | ||||
Page 3 of 3
Finding DataMost applications of jQuery data or the Dataset API require you to find and process elements that have data set on them. This is fairly easy using the attribute selectors. The only complication is that you need to keep in mind that you have to use the dashed form of the name to find the DOM elements and the camel-case form in the JavaScript. For example, to extract all DOM objects that have an attribute data-mydata-x you would use: alert($("[data-mydata-x]"); and to get the value of the data you would use: alert($("[data-mydata-x]").data("mydataX")); See the section on Attribute selectors in Chapter 3. You can, of course, find only the DOM objects that have a data- attribute set to a particular value or that contains a substring. For example alert($("[data-mydata-x*='my']"); selects only elements with a data-mydata-x attribute with a value that contains the substring my. Using attribute selectors you can pick out any DOM objects with a given attribute name and a value that is specified, prefixed, ends or contains a specific word. This is flexible, but often you need to get all the elements that have a data-* attribute of any sort. The problem is jQuery doesn't provide any way to select elements with attribute names that match a pattern. It is fairly easy to create a filter function that will filter a results list to only include elements that have data. There is a test function, hasData, that returns true if an element has data and false otherwise. This can be used in a filter function to reduce a result array to just the elements that have data: var has= $("button").filter(function(){ return $.hasData(this); }); This finds all button objects and then filters out all of those that don't have data. If you try it out you will find that it only filters elements that you have used .data() to actually set data. That is, it doesn't filter out elements that have data-* attributes. The hasData function is testing to see if jQuery has stored data, not that there is a data-* attribute on the original tag. Notice that jQuery uses its data storage mechanism for internal tasks such as storing information about event handlers that are attached to an element. This means that you might get back elements that you didn't expect to be storing data from the filter function. There are many possible ways of testing to see if an element has any data-* but the simplest is to check to see if the DOM object has a non-empty dataset property: var has= $("button").filter(function(){ return $.isEmptyObject($(this).prop("dataset")); }); This looks complicated but all that happens is that first we find all button elements and filter the results. The filter function simply finds the dataset property - all DOM objects have a dataset property - and then we use the utility function isEmptyObject to check if there is any data stored in the property. This reduces the list to only those elements that have data defined using data-* tags. Notice that this does not select elements that have had data set using nothing but the .data function as this does not add entries to the dataset property. Included in chapter but not in this extract
Summary
More Information Available as a Book:
buy from Amazon
Also Available:buy from Amazon To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info <ASIN:1871962501> <ASIN:1871962528> <ASIN:1871962560> |
||||
Last Updated ( Sunday, 15 October 2023 ) |