3

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?

Smax Smaxović
  • 550
  • 2
  • 7
  • 17

1 Answers1

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.

  1. 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;

  2. 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