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
2422 questions
446
votes
6 answers

Do DOM tree elements with IDs become global properties?

Working on an idea for a simple HTMLElement wrapper I stumbled upon the following for Internet Explorer and Chrome: For a given HTMLElement with an id in the DOM tree, it is possible to retrieve the
using its ID as a variable name or as a…
KooiInc
  • 119,216
  • 31
  • 141
  • 177
344
votes
9 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
  • 9,119
  • 12
  • 60
  • 109
270
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
  • 11,797
  • 20
  • 71
  • 87
233
votes
27 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
  • 2,500
  • 2
  • 14
  • 12
204
votes
8 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
  • 3,232
  • 6
  • 21
  • 23
200
votes
2 answers

querySelector vs. getElementById

I have heard that querySelector and querySelectorAll are new methods to select DOM elements. How do they compare to the older methods, getElementById and getElementsByClassName in terms of performance and browser support? How does the performance…
goesToEleven
  • 2,083
  • 2
  • 12
  • 4
145
votes
5 answers

Selecting an element in iframe with 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 token IDs. I need to select the elements by those token IDs. Means - I click on an element on the main page and…
cycero
  • 4,547
  • 20
  • 53
  • 78
131
votes
9 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
  • 3,312
  • 10
  • 44
  • 66
103
votes
8 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
  • 1,033
  • 2
  • 8
  • 4
91
votes
6 answers

How to pick element inside iframe using document.getElementById

I have a iframe like this