Yuhuan Jiang
Yuhuan Jiang

Reputation: 2726

How to enumerate all custom HTML tags using javascript?

Say I have the following in my HTML file

<div class="foo"> item1 </div>
<div class="foo"> item2 </div>
<div class="foo"> item3 </div>

I can easily get all these divs using the following JavaScript code:

document.getElementsByClassName("foo")

Here, foo is defined somewhere in a CSS file as follows:

.foo { /* Some style */ }

Now, the question is, sometimes one would define foo as a custom HTML tag as follows

foo { /* Some style */ }

to allow for more succinct HTML:

<foo> item1 </foo>
<foo> item2 </foo>
<foo> item3 </foo>

But then the JavaScript line above does not work anymore. How should one get all the foo nodes in this case?

Upvotes: 3

Views: 168

Answers (2)

vijay
vijay

Reputation: 631

use same class like this <foo class="foo"> item1 </foo> <foo class="foo"> item2 </foo> <foo class="foo"> item3 </foo>

or use

document.getElementsByTagName("foo")

Upvotes: 1

andreas
andreas

Reputation: 16936

You can just use:

document.getElementsByTagName("foo");

See MDN for more information.

Upvotes: 3

Related Questions