How can I get the value of an HTML hidden field with a JavaScript?

Posted on 2011-05-05
Last Modified: 2012-05-11
Dear Experts:

Does anyone knows how to get the value of an HTML field defined as:

<INPUT NAME="myHiddenField" TYPE="hidden" VALUE="My Value">

Open in new window

I'm trying to retrieve the value of this field using JavaScript.  I will appreciate your reply.


Question by:Conrado ZAVALA
    LVL 9

    Accepted Solution

    LVL 81

    Assisted Solution

    If there's no ID attribut, we may use the name.
    Assuming there's only one element with this name (myHiddenField) or it's the first one found in the DOM ([0]) we may use getElementsByTagName :

    var the_value = document.getElementsByName("myHiddenField")[0].value

    Author Comment

    by:Conrado ZAVALA
    Thank you "erdincqc" and "leakim971" for your anwers.  Is "getElementsByName( )" compatible with all browsers?

    LVL 9

    Expert Comment

    Yes that will a reliable usage .
    LVL 81

    Expert Comment

    yes, the correct lin is :
    Check browser compatibility

    ...but not getElementsByClassName which is not available on IE
    LVL 9

    Expert Comment

    i said Reliable .. modern browsers ok , but i've forgot IE :) ...I realized that ie7 is not working always..

    Look at here ;

    When you use the getElementsByName method, all elements in the document that have the specified NAME attribute or ID attribute value are returned.

    Elements that support both the NAME attribute and the ID attribute are included in the collection returned by the getElementsByName method, but elements with a NAME expando are not included in the collection; therefore, this method cannot be used to retrieve custom tags by name.

    Firefox allows getElementsByName() to retrieve elements that use a NAME expando, which is why it works. Whether or not that is a Good Thing™ may be up for debate, but that is the reality of it.

    So, one option is to use the getAttribute() DOM method to ask for the NAME attribute and then test the value to see if it is what you want, and if so, add it to an array

    You'd better use "id"  and btw ; Yes there should be an index [0] after getElementsByName to choose one of the same named elements .Like radio buttons

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    Digital marketing agencies have encountered both the opportunities and difficulties that emerge from working with a wide-ranging organizations.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now