开发者

how to get hidden element value in javascript

how to ge开发者_开发知识库t hidden element value in javascript


using the following document function

document.getElementById("elementId").value;

elementId-> id defined for the hidden element


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 textContent property

document.getElementById( "hidElem" ).textContent;

for a hidden div element like

<div style="display: none;" id="hidElem">value of hidden element</div>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜