Should I not use data attributes in html? -
i coding c# forms application lets user add custom html nodes html webpage. have javascript code selects html node execute specific code objects such jquery ui slider.
to identify html nodes, need store attribute in tag identifies tag.
i new writing html code, , such, ask if there reason why should not use data attributes tags? there limitations disadvantages should aware of?
here example code have working:
<!doctype html> <html> <head> </head> <body> <div data-customwebpagelayout-name='slider-increments'> <div data-customwebpageobject-name='slider-increments'></div> </div> <p data-customwebpageobject-output-name='slider-increments'>1</p> </body> </html>
thank you.
the common way identify , select html tags either class
or id
.
if there going 1 of on page, should use id
. looks have multiple of same thing want identify, i'd use class
so:
<!doctype html> <html> <head> </head> <body> <div class="customwebpagelayout slider-increments" > <div class="customwebpageobject slider-increments"></div> </div> <p class="customwebpageobject slider-increments">1</p> </body> </html>
then select nodes javascript so:
document.getelementsbyclassname("slider-increments");
or if decide use jquery:
$(".slider-increments");
data attributes more of pain work with:
raw javascript (code adapted code in answer):
var matchingelements = []; var allelements = document.getelementsbytagname('*'); (var = 0, n = allelements.length; < n; i++){ if (allelements[i].getattribute('data-customwebpagelayout-name') !== null){ // element exists attribute. add array. matchingelements.push(allelements[i]); } } //use matchingelements;
jquery:
$("*[data-customwebpagelayout-name='data-customwebpagelayout-name']");
Comments
Post a Comment