Questions tagged [getelementsbytagname]

A native HTML DOM Element method used to get all elements with the specified tag name.

Description

getElementsByTagName is a native HTML DOM Element method used to get all elements with the specified tag name.

The returned list is live, meaning that it updates itself with the DOM tree automatically. Consequently, there is no need to call several times element.getElementsByTagName with the same element and arguments.

Syntax

elements = element.getElementsByTagName(tagName);

where,

  • elements is a live NodeList (but see the note below) of found elements in the order they appear in the subtree. If no elements were found, the NodeList is empty.
  • element is the element from where the search should start. Note that only the descendants of this element are included in the search, but not the element itself.
  • tagName is the qualified name to look for. The special string "*" represents all elements. For compatibility with XHTML, lower-case should be used.

Specifications & References

  1. W3C Specification
  2. getElementsByTagName - MDN Link
583 questions
98
votes
10 answers

How to loop through all the elements returned from getElementsByTagName

I am trying to loop through all the elements retruned from getElementsByTagName("input") using forEach. Any ideas why this does not work in FF, Chrome or IE?
slayernoah
  • 4,382
  • 11
  • 42
  • 73
73
votes
5 answers

Can I select multiple tags using getElementsByTagName?

I'm using a javascript snippet in order for visitors to my site to increase the font size on all paragraphs using the following javascript: function increaseFontSize() { var paragraphs = document.getElementsByTagName('p'); …
biddybump
  • 733
  • 1
  • 5
  • 6
66
votes
2 answers

JavaScript getElement by href?

I've got the script below var els = document.getElementsByTagName("a"); for(var i = 0, l = els.length; i < l; i++) { var el = els[i]; el.innerHTML = el.innerHTML.replace(/link1/gi, 'dead link'); } However this searches through the page and…
owenmelbz
  • 6,180
  • 16
  • 63
  • 113
59
votes
4 answers

How to insertBefore() element in body tag?

I am trying to use insertBefore in js like this: var p = document.createElement("p"); p.innerHTML = "test1"; document.body.insertBefore(p, null); var p = document.createElement("p"); p.innerHTML = "test2"; document.body.insertBefore(p, null); But…
slemdx
  • 1,055
  • 2
  • 16
  • 19
48
votes
7 answers

How do I get all h1,h2,h3 etc elements in javascript?

I want to write something like this in javascript: var all_headings = document.getElementsByTagName("h1,h2,h3,h4,h5,h6"); all_headings would then be a list of all elements that are h1 or h2 or h3... And in the order that they appear in the…
Eyal
  • 5,728
  • 7
  • 43
  • 70
29
votes
2 answers

Use vanilla javascript to add / remove class to a different element after clicking on another one

I have looked through a number of similar questions but can not find a specific example of one that answers in vanilla JS how to add and remove a class to a different element from the one clicked on. I know it has something to do with setting up a…
20
votes
4 answers

Why can I not remove a child element I've just found? NOT_FOUND_ERR

I'm building a script which has to patch XML files, including replacing one list of elements with another. The following function applies a patch (involving a possibly empty list of elements with the same name) onto a parent Element's list of…
dhardy
  • 11,175
  • 7
  • 38
  • 46
18
votes
3 answers

GetElementsByTagName in Htmlagilitypack

How do I select an element for e.g. textbox if I don't know its id? If I know its id then I can simply write: HtmlAgilityPack.HtmlNode node = doc.GetElementbyId(id); But I don't know textbox's ID and I can't find GetElementsByTagName method in…
Ali
  • 1,801
  • 6
  • 43
  • 58
16
votes
2 answers

Is it possible to make querySelectorAll live like getElementsByTagName?

getElementsByTagName() has 2 great features: it is fast and it is live. But what if I want to get p strong. Of course I could refine a selection using getElementsByTagName() again but wouldn't I lose the live effect for the new p tags? Is there a…
15
votes
2 answers

javascript TypeError: document.getElementsByTagName("p")[0].innerHtml is not a function

I'm getting the following error for a simple function below: TypeError: document.getElementsByTagName("p")[0].innerHtml is not a function I'm just trying to understand the usage of getElementsByTagName. function…
vaanipala
  • 1,261
  • 7
  • 36
  • 63
12
votes
9 answers

Recommended method to locate the current script?

I am writing a script that needs to add DOM elements to the page, at the place where the script is located (widget-like approach). What is the best way to do this? Here are the techniques I am considering: Include an element with an id="Locator"…
Christophe
  • 27,383
  • 28
  • 97
  • 140
11
votes
5 answers

Change style of all elements using getElementsByTagName()

I'm fairly new to javascript and have been unable to get this code to work and I am unsure were and what I'm missing. So here is what I want it to do. I'm trying to have the script read everything and switch the visibility of the span found in the…
pancakes
  • 113
  • 1
  • 1
  • 5
9
votes
4 answers

How to insert metatag without using jquery append?

I used the following jquery to insert a metatag into a html document.