Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting HTML elements by their attribute names

There are methods available in JavaScript to get HTML elements using their ID, Class and Tag.

document.getElementByID(*id*); document.getElementsByClassName(*class*); document.getElementsByTagName(*tag*); 

Is there any method available to get the elements according to the attribute name.

EX:

<span property="v:name">Basil Grilled Tomatoes and Onions</span> 

Like:

document.getElementsByAttributeName("property"); 
like image 209
Saurabh Saxena Avatar asked Oct 04 '11 11:10

Saurabh Saxena


People also ask

How do you get all the elements of a specified element by its tag name?

The Element. getElementsByTagName() method returns a live HTMLCollection of elements with the given tag name. All descendants of the specified element are searched, but not the element itself. The returned list is live, which means it updates itself with the DOM tree automatically.

How do I get attributes in HTML?

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 I use a name attribute in querySelector?

Use the querySelector() method to get an element by a name attribute, e.g. document. querySelector('[name="first_name"]') . The method returns the first element in the DOM that matches the provided selector. If no element matches the selector, null is returned.

How do I get the HTML element name?

Right-click on the element. Click on Inspect within the popup menu. A preview window will popup highlighting the webpage's HTML. There you'll be able to find the HTML ID or Name for that element.


2 Answers

Yes, the function is querySelectorAll (or querySelector for a single element), which allows you to use CSS selectors to find elements.

document.querySelectorAll('[property]'); // All with attribute named "property" document.querySelectorAll('[property="value"]'); // All with "property" set to "value" exactly. 

(Complete list of attribute selectors on MDN.)

This finds all elements with the attribute property. It would be better to specify a tag name if possible:

document.querySelectorAll('span[property]'); 

You can work around this if necessary by looping through all the elements on the page to see whether they have the attribute set:

var withProperty = [],     els = document.getElementsByTagName('span'), // or '*' for all types of element     i = 0;  for (i = 0; i < els.length; i++) {     if (els[i].hasAttribute('property')) {         withProperty.push(els[i]);     } } 

Libraries such as jQuery handle this for you; it's probably a good idea to let them do the heavy lifting.

For anyone dealing with ancient browsers, note that querySelectorAll was introduced to Internet Explorer in v8 (2009) and fully supported in IE9. All modern browsers support it.

like image 84
lonesomeday Avatar answered Nov 03 '22 15:11

lonesomeday


In jQuery this is so:

$("span['property'=v:name]"); // for selecting your span element 
like image 29
IceMan Avatar answered Nov 03 '22 16:11

IceMan