Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript get element by value [duplicate]

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?

like image 288
Smax Smaxović Avatar asked Jun 11 '26 11:06

Smax Smaxović


1 Answers

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;
}
like image 171
Rahul Joshi Avatar answered Jun 14 '26 01:06

Rahul Joshi