Just jQuery The Core UI - Data |
Written by Ian Elliot | ||||
Sunday, 15 October 2023 | ||||
Page 1 of 3 There are some hidden aspects of using jQuery and its data manipulation functions represent buried treasure. If you have struggled to work with data in JavaScript, and with the DOM in particular, jQuery has some functions you really need to know about. This is an extract from my book Just jQuery The Core UI. Available as a Book:
buy from Amazon
Also Available:buy from Amazon We generally don't think too much about data in HTML and JavaScript. It is something that is the job of a database or perhaps JSON and Ajax. However, in modern HTML-based apps it is common to embed data within tags that is then transferred to the corresponding DOM elements. The idea is that this allows the UI to pass data to the code and this can be used to modify how the code deals with the UI. In modern frameworks this is used to implement extensions to UI elements and features such as data binding. First we need to look at the underlying HTML5 dataset API. Dataset APIUntil HTML5, the way that data was passed from HTML tags to the DOM elements was ad-hoc. You could add any attribute to an HTML tag and it would be converted to an attribute of the corresponding DOM object. For example, if you use the tag: <button mydata="myvalue">test1</button> then an IDE such as NetBeans will flag an error because there is no mydata attribute in a button tag. However, if you load the page you will discover that everything works and the DOM object corresponding to the button has a mydata attribute with the value "myvalue". For example if you try: alert($("button")[0].getAttribute("mydata")); to get the button DOM element, the [0] converts the jQuery object to a DOM object, so if you use the DOM getAttribute method you will discover that it returns the value "myvalue". This also works if you use nothing but jQuery: alert($("button").attr("mydata")); which also displays "myvalue". The reason for showing the two versions is to emphasize that this isn't anything that jQuery is doing. JavaScript supports "expando" properties, which roughly means that you can add properties to JavaScript objects simply by using them. In the same way, the JavaScript objects created to represent HTML tags also have expando properties and they will also add properties for any non-standard attributes that you include in the tag. This is a useful idea, but it lacks any organization. HTML5 introduced a system for naming and using custom attributes as data, the Dataset API. In this the name of the custom attribute always has to start data- so our previous example would have to be data-mydata. Simple enough, but there is one more twist to the story. When the attributes are converted to JavaScript the “data” and the dashes are removed. So in JavaScript data-mydata becomes mydata. Each dash that is removed after the first results in the next letter being converted to upper case. So data-mycompany-mydata becomes mycompanyMydata. Notice that you can add additional dashes to build up unique or more complex naming schemes. The tag version of the name is often referred to as "dashed" and the property version is called "camel-cased" because of the way the upper case characters stick up in the middle of the word. To make getting at the data easier, the API also adds a new dataset property to all DOM elements which is set to an object containing key value pairs. For example, following: <button data-mydata-x="myvalue" data-mydata-y="10" data-mydata-z=20"> test1 </button> you can retrieve the data from the DOM element using: alert($("button")[0].dataset.mydataX); alert($("button")[0].dataset.mydataY); alert($("button")[0].dataset.mydataZ); That is, dataset is an object with properties mydataX, mydataY and mydataZ. You can access the dataset object directly from jQuery but notice that it is a property not an attribute, so you would need to use something like: alert($("button").prop("dataset").mydataX);
alert($("button").prop("dataset").mydataY);
alert($("button").prop("dataset").mydataZ);
To summarize:
|
||||
Last Updated ( Sunday, 15 October 2023 ) |