I have an HTML 5 page with several articles. Each article has a custom attribute titled “MyAttribute”:
<html>
<article MyAttribute="x">
<article MyAttribute="y">
<article MyAttribute="z">
</html>
I would like to create a function that does something like the following pseudo-code:
for each article {
get MyAttribute from the article
if MyAttribute == x {do this}
if MyAttribute == y {do that}
if MyAttribute == z {do something else}
}
What is the most efficient way to do this using jQuery/JavaScript?
Instead of using custom attributes, I’d recommend placing your “MyAttribute” in a data-* attribute like this:
This creates “more valid” HTML. Then you would write javascript like this:
I’ve also created a jsFiddle of the result for you.