Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementsByAttribute() is not a function

I have the following code to get an element by it's attribute:

document.getElementsByAttribute("xlink:href",id);

I get this error:

Uncaught TypeError: document.getElementsByAttribute is not a function

I don't understand what's going wrong, is it not a standard chrome function?

like image 566
Arihant Avatar asked Jul 07 '16 19:07

Arihant


People also ask

What is getAttribute in js?

The getAttribute() method of the Element interface returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string); see Non-existing attributes for details.

How do I find the value of a DOM element?

HTML DOM getAttribute() method is used to get the value of the attribute of the element. By specifying the name of the attribute, it can get the value of that element. To get the values from non-standard attributes, we can use the getAttribute() method.

How do you use getAttribute?

How it works: First, select the link element with the id js using the querySelector() method. Second, get the target attribute of the link by calling the getAttribute() of the selected link element. Third, show the value of the target on the Console window.


1 Answers

From documentation:

Note that this method is only available on XUL elements; it is not part of the W3C DOM.

Use this:

document.querySelectorAll("[xlink|href='"+id+"']");
like image 134
Niet the Dark Absol Avatar answered Sep 20 '22 17:09

Niet the Dark Absol