Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

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

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.

Sincerely,

Conrado
0
Conrado ZAVALA
Asked:
Conrado ZAVALA
  • 3
  • 2
2 Solutions
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Simply
myfieldvalue=document.getElementsByName('myHiddenField').value;
0
 
leakim971PluritechnicianCommented:
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 : http://www.w3schools.com/Dom/met_document_getelementsbytagname.asp

var the_value = document.getElementsByName("myHiddenField")[0].value
0
 
Conrado ZAVALAAuthor Commented:
Thank you "erdincqc" and "leakim971" for your anwers.  Is "getElementsByName( )" compatible with all browsers?

Conrado
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Yes that will a reliable usage .
0
 
leakim971PluritechnicianCommented:
yes, the correct lin is : http://www.w3schools.com/jsref/met_doc_getelementsbyname.asp
Check browser compatibility

...but not getElementsByClassName which is not available on IE
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
i said Reliable .. modern browsers ok , but i've forgot IE :) ...I realized that ie7 is not working always..

Look at here ; http://msdn.microsoft.com/en-us/library/ms536438(VS.85).aspx

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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now