Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get value by class name using javascript [duplicate]

Tags:

javascript

Sorry, it's basic one but I trying to search on google anyways but still not get success.

I want get value of this

<input type='hidden' class='hid_id' value='1' />

Using Javascript I want alert value 1

I trying this

var id = document.getElementsByClassName("hid_id");
alert (id);

But it's alert [object HTMLInputElement]

please help me now.

like image 777
Songs Avatar asked Dec 10 '15 17:12

Songs


2 Answers

try this:

var id = document.getElementsByClassName("hid_id")[0].value;
like image 81
madox2 Avatar answered Sep 28 '22 00:09

madox2


getElementsByClassName() returns an array so you have to access first element (if there is any). Then try accessing value property:

var id = document.getElementsByClassName("hid_id");
if (id.length > 0) {
    alert (id[0].value);
}

jsfiddle

like image 29
Keammoort Avatar answered Sep 28 '22 02:09

Keammoort