I want to get HTML element by it's value using Javascript.
I thought that there is function like getElementByValue()
but there isn't.
How I can do it?
Asked
Active
Viewed 4.1k times
3

Smax Smaxović
- 550
- 2
- 7
- 17
-
See this similar SO question: http://stackoverflow.com/questions/8926378/how-to-select-an-input-element-by-value-using-javascript – Kurtis Zimmerman Mar 22 '14 at 14:25
1 Answers
2
I don't know what exactly you want to achieve, but following are the ways you can use to get value of an element.
Select element using id and then get value of it. This works cross browser.
var my_id = document.getElementById("my_id"); var my_value = my_id.value;
Select element using class name (this works on all modern browser but not on ie8 and below)
var my_class = document.getElementsByClassName("my_class"); var count = my_class.length; for(var i = 0; i < count; i++){ my_class[i].value; }
3.Select element using tag name
var my_tag = document.getElementsByTagName("my_tag");
var count = my_tag.length;
for(var i = 0; i < count; i++){
my_tag[i].value;
}

Rahul Joshi
- 193
- 4