• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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