3

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?

3
  • 3
    document.getElementsByTagName('foo') does not work for you? Commented Nov 25, 2016 at 20:17
  • <foo> won't be a valid html tag unless you define it otherwise. developer.mozilla.org/en-US/docs/Web/Web_Components/… Commented Nov 25, 2016 at 20:17
  • It works! @ettanany Commented Nov 25, 2016 at 20:19

2 Answers 2

3

You can just use:

document.getElementsByTagName("foo");

See MDN for more information.

Sign up to request clarification or add additional context in comments.

Comments

1

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.