Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing elements by type in JavaScript

A while ago I was making some test in JavaScript, and played with a code to get the text of all elements with a certain class.

Now I was trying to make something like this but obtain all elements by a certain type, for example all elements type="text"

Is there any way to do this in JavaScript or should I use jQuery?

var xx = document.getElementsByClassName("class"); for (i=0;i<xx.length;i++){     var str=xx[i].innerHTML;             alert(str); } 
like image 830
Saikios Avatar asked May 05 '11 11:05

Saikios


People also ask

How do you access elements in JavaScript?

In JavaScript, we can use the getElementsByTagName() method to access all the elements with the given tag name. This method is the same as the getElementsByName() method. Here, we are accessing the elements using the tag name instead of using the name of the element.

What is get element by tagName?

Definition and Usage The getElementsByTagName() method returns a collection of child elements with a given tag name. The getElementsByTagName() method returns a NodeList object.

What is element type in JavaScript?

PreviousNext. The Element type represents an XML or HTML element, providing access to information such as its tag name, children, and attributes.


1 Answers

If you are lucky and need to care only for recent browsers, you can use:

document.querySelectorAll('input[type=text]') 

"recent" means not IE6 and IE7

like image 122
Mic Avatar answered Sep 25 '22 19:09

Mic