Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementById doesn't work on a node

In this simple script i get the error "obj.parentNode.getElementById is not a function", and I have no idea, what is wrong.

<script type="text/javascript">

        function dosomething (obj) {
         sibling=obj.parentNode.getElementById("2");
         alert(sibling.getAttribute("attr"));
        }

</script>

<body>
 <div>
  <a id="1" onclick="dosomething(this)">1</a>
  <a id="2" attr="some attribute">2</a>
 </div>
</body>
like image 844
Ursus Russus Avatar asked Oct 10 '10 23:10

Ursus Russus


People also ask

Can I use document getElementById in node?

how can I use document. getElementById() in nodejs ? Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.

Why is document getElementById not working?

The reason behind document. getElementByID is not a function Solution. The JavaScript getElementById method selects an element from the HTML Document Object Model (DOM). If you incorrectly spell this method, you'll encounter the document.

Should I use querySelector or getElementById?

You should opt to use the querySelector method if you need to select elements using more complex rules that are easily represented using a CSS selector. If you want to select an element by its ID, using getElementById is a good choice.

Why is node js not working?

Problems occurring in Node. js application deployments can have a range of symptoms, but can generally be categorized into the following: Uncaught exception or error event in JavaScript code. Excessive memory usage, which may result in an out-of-memory error.


2 Answers

.getElementById() is on document, like this:

document.getElementById("2");

Since IDs are supposed to be unique, there's no need for a method that finds an element by ID relative to any other element (in this case, inside that parent). Also, they shouldn't start with a number if using HTML4, a numberic ID is valid in HTML5.

like image 196
Nick Craver Avatar answered Sep 22 '22 17:09

Nick Craver


replace .getElementById(id) with .querySelector('#'+id);

like image 39
kernowcode Avatar answered Sep 20 '22 17:09

kernowcode