How to get hidden field value in JavaScript

In JavaScript, you can use following two ways to get hidden field value in a form :

  1. document.getElementById(‘hidden field id’).value
  2. document.formName.elements[‘hidden field name’].value

See an example here…


<html>
<body>

<script type="text/javascript">
function printIt(){
   alert(document.getElementById('abcId').value);
   alert(document.formName.elements['abcName'].value);
}
</script>

<h1>Access Hidden value in JavaScript</h1>
<form name="formName">
	<input type=hidden id="abcId" name="abcName" 
                  value="Wall Street! Money Never Sleep"/>

	<input type=button value="Get Value" onclick="printIt()" />
</form>

</body>
</html>

If the button is clicked, it will prompts the hidden field value “Wall Street! Money Never Sleep”.

11 comments on “How to get hidden field value in JavaScript

  1. Heya im for the first time the following. I discovered this specific table as a consequence I to discover The item faithfully of use &amp the idea rallied round myself publicized lots. I am hoping to deliver a little back again furthermore foster further like so adaebkeebkkddbbf

    Reply
  2. It is better practice to use something like this.

    document.getElementById(”).value

    This way it will work if you use Master Pages as well.

    Reply
  3. Hi mkyong,
    This tutorial is very help but can you tell me how can i get an array of all input type elements on jsp in javascript.My elements are created dynamically.

    Reply
  4. Hi , Do you have any suggestion on huge data transfer
    I have very large XML’s to be transfered from offline to online server.
    I idea is to apply some shrinking logic and make the xml shrink and send across,.
    Can you guide me on your brain thoughts to carry out a safe and easy data transfer.
    Regards
    Ganesh

    Reply
  5. Hello,

    Man, you don’t know how many times your site helped me.
    Thank you again and keep the good work!

    Reply
  6. If one checkbox is disbaled how to get the value checkbox in backingbean in jsf without using valueChangeListener …

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *