Questions tagged [getelementbyid]

getElementById is an essential method commonly used in JavaScript in the browser to retrieve a particular element node in a HTML or XML document by its ID.

Description

getElementById is an essential method commonly used in JavaScript in the browser to retrieve a particular element node in a HTML or XML document by its ID.

Syntax

element = document.getElementById(id);

where,

  • element is a reference to an Element object, or null if an element with the specified ID is not in the document.
  • id is a case-sensitive string representing the unique ID of the element being sought.

References

  1. W3C Specification
  2. getElementById - MDN Link
2191 questions
396
votes
5 answers

Do DOM tree elements with ids become global variables?

Working on an idea for a simple HTMLElement wrapper I stumbled upon the following for Internet Explorer and Chrome: For a given HTMLElement with ID in the DOM tree, it is possible to retrieve the div using its ID as the variable name. So for a div…
KooiInc
  • 104,388
  • 28
  • 131
  • 164
240
votes
8 answers

How to access a DOM element in React? What is the equilvalent of document.getElementById() in React

How do I select certain bars in react.js? This is my code: var Progressbar = React.createClass({ getInitialState: function () { return { completed: this.props.completed }; }, addPrecent: function (value) { …
user504909
  • 6,633
  • 8
  • 49
  • 83
230
votes
7 answers

Getting the parent div of element

This should be really simple but I'm having trouble with it. How do I get a parent div of a child element? My HTML:

Testing

My JavaScript: var pDoc = document.getElementById("myParagraph"); var…
OVERTONE
  • 10,737
  • 20
  • 62
  • 86
162
votes
6 answers

Get element inside element by class and ID - JavaScript

Alright, I've dabbled in JavaScript before, but the most useful thing I've written is a CSS style-switcher. So I'm somewhat new to this. Let's say I have HTML code like this:
Hello world! …
Tanner Babcock
  • 2,767
  • 6
  • 18
  • 23
135
votes
17 answers

Cannot read property 'addEventListener' of null

I have to use vanilla JavaScript for a project. I have a few functions, one of which is a button that opens a menu. It works on pages where the target id exists, but causes an error on pages where the id doesn't exist. On those pages where the…
morocklo
  • 1,500
  • 2
  • 11
  • 12
134
votes
5 answers

Selecting an element in iFrame jQuery

In our application, we parse a web page and load it into another page in an iFrame. All the elements in that loaded page have their tokenid-s. I need to select the elements by those tokenid-s. Means - I click on an element on the main page and…
cycero
  • 4,075
  • 17
  • 48
  • 73
95
votes
7 answers

How to getElementByClass instead of GetElementById with JavaScript?

I'm trying to toggle the visibility of certain DIV elements on a website depending on the class of each DIV. I'm using a basic JavaScript snippet to toggle them. The problem is that the script only uses getElementById, as getElementByClass is not…
Alan
  • 953
  • 1
  • 7
  • 4
94
votes
8 answers

Javascript getElementById based on a partial string

I need to get the ID of an element but the value is dynamic with only the beginning of it is the same always. Heres a snippet of the code.
The ID always starts with poll- then…
calebo
  • 2,758
  • 7
  • 37
  • 65
76
votes
6 answers

Javascript Append Child AFTER Element

I would like to append an li element after another li inside a ul element using javascript, This is the code I have so far.. var parentGuest = document.getElementById("one"); var childGuest = document.createElement("li"); childGuest.id = "two"; I…
Wez
  • 10,021
  • 5
  • 47
  • 62
76
votes
5 answers

How to pick element inside iframe using document.getElementById

I have a iframe like this