JQuery .data() not working?

data doesn’t set data-* attributes. It manages a data cache unrelated to data-* attributes. It initializes from data-* attributes if there are any present, but never writes to them. To write to an attribute, use attr. Example: Updated Fiddle var div = $(“<div />”) $(div).attr(“data-foo”, “bar”) console.log($(div)[0].outerHTML) What you’re seeing is just one of the … Read more

jQuery: Select data attributes that aren’t empty?

Just as further reference, and an up-to-date (may’14) (aug’15) (sep’16) (apr’17) (mar’18) (mar’19) (may’20) (jan’22)… Answer that works with: ###Empty strings: If the attr must exist & could have any value (or none at all) jQuery(“[href]”); ###Missing attributes: If attr could exist & if exist, must have some value jQuery(“[href!=”]”); ###Or both: If attr must … Read more

Is there any problem with using HTML5’s “data-*” attributes for older browsers?

There isn’t really, they’re not 100% correct/valid usage in HTML4 of course….but they don’t cause problems either, so they’re still a great way to solve the “I need an attribute for this” problem. If it helps, I’ve used these while supporting IE6 and have had zero issues thus far, and I can’t recall a single … Read more

How to set data attributes in HTML elements

HTML <div id=”mydiv” data-myval=”10″></div> JS var a = $(‘#mydiv’).data(‘myval’); //getter $(‘#mydiv’).data(‘myval’,20); //setter Demo Reference From the reference: jQuery itself uses the .data() method to save information under the names ‘events’ and ‘handle’, and also reserves any data name starting with an underscore (‘_’) for internal use. It should be noted that jQuery’s data() doesn’t change … Read more

Why should I prepend my custom attributes with “data-“?

There are several benefit for keeping custom attributes prefixed with data-*. It guarantees there will not be any clashes with extensions to HTML in future editions. This is a problem that has been encountered to some degree already with some of the new attributes introduced in HTML5, where existing sites were using attributes with the … Read more

techhipbettruvabetnorabahisbahis forumu