tags:

views:

51

answers:

3

how to get hidden element value in javascript

+2  A: 

If the element has an id attribute and if it has a value attribute then you can use value property

document.getElementById ( "hidElem" ).value;

for a hidden input element like

<input type='hidden' id='hidElem' />

otherwise you can use innerText property

document.getElementById ( "hidElem" ).innerText;

for a hidden div element like

<div style='display: none' id='hidElem'>value of hidden element</div>
rahul
+3  A: 

using the following document function

document.getElementById("elementId").value;

elementId-> id defined for the hidden element

A: 

Putting ids on all your elements is overkill in my opinion. You can access them by their name attribute - all you need is a reference to the form object.

<form id="blah">
    <input type="hidden" name="inputOne" value="1" />
    <input type="hidden" name="inputTwo" value="2" />
</form>


var formObj = document.getElementById('blah');
alert("Input one value: " + formObj.inputOne.value);
alert("Input two value: " + formObj.inputTwo.value);

This applies to all types of form input.

nickf