?
Solved

Issues with Hidden fields (HTML/Javascript)

Posted on 2009-02-12
5
Medium Priority
?
1,524 Views
Last Modified: 2012-06-27
Hi there,
I'm writing some code in ASP to generate HTML.
Part of what I'm trying to do requires me to have a hidden field containing the PK of the line of the combobox.

I've tried setting an onchange event, and setting the hidden field directly, but it didn't work. Neither did writing a javascript routine to do it.

Looking at it further, when I ran the code in the microsoft developper, it saw all the other tags, except the hidden one.

I'm not sure how to make the change, so any help would be great. I'm attaching the html created by the asp.

Thanks
Eric
<script type='text/javascript'>
function change() 
{
	document.getElementById('txtID').value='5';
	return;
}
</script>
 
<html>
<head>
	<title>WSPCHS Congress - Please Confirm User</title>
</head>
<BODY>
<p>More than one user appears with that username.</p>
<p>Please confirm which one is you:</p>
<form name="frmName" method="post" action="">
  <p>
 
  <input type="hidden" name="txtID">  
  <select name="cmbUserlist" onchange="change()">
    
       <OPTION VALUE=2>Dr.
Bruce
Wayne, Gotham, New york, United States
       <OPTION VALUE=999>Mr.
Bruce
Wayne, earth2, earth2, United States    
    </select>
  </p> 
  <p> 
    <input type="submit" name="Submit" value="Confirm">
    <input type="submit" name="Submit2" value="New User">
    <input type="submit" name="Submit3" value="Back">
  </p>
 
</form>
<p>&nbsp;</p>
</BODY>
</html>

Open in new window

0
Comment
Question by:saragose
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Morcalavin
ID: 23626419
Name and ID are two different properties
<input type="hidden" name="txtID">   has no id, so getElementById won't work
<input type="hidden" name="txtID" id="txtID">    now you can use getElementById

0
 

Author Comment

by:saragose
ID: 23627229
Hey there,
I've tried a few variations, but with no success.
Ideally I would like to be able to have my hidden field be something like:
<input type="hidden" name="txtID" value='Test'>
and my routine say something like:
<script type='text/javascript'>
function change()
{
    document.frmName.txtID.value='5';
    return;
}
</script>

Unfortunately that doesn't seem to work, and I pull up the values assinged to the hidden fields using alerts. I also tried using the ID and getElementByID, but I can't seem to set the value of the hidden field that way.

Thanks
Eric
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 23627388
Maybe I'm not understanding the problem, because the following code works fine for me at seeing the value of the hidden field.

<script type='text/javascript'>
function change() 
{
        document.frmName.txtID.value='5';
        alert(document.getElementById('txtID').value);
}
</script>
 
<html>
<head>
        <title>WSPCHS Congress - Please Confirm User</title>
</head>
<BODY>
<p>More than one user appears with that username.</p>
<p>Please confirm which one is you:</p>
<form name="frmName" method="post" action="">
  <p>
 
  <input type="hidden" name="txtID" id="txtID">  
  <select name="cmbUserlist" onchange="change()">
    
       <OPTION VALUE=2>Dr.
Bruce
Wayne, Gotham, New york, United States
       <OPTION VALUE=999>Mr.
Bruce
Wayne, earth2, earth2, United States    
    </select>
  </p> 
  <p> 
    <input type="submit" name="Submit" value="Confirm">
    <input type="submit" name="Submit2" value="New User">
    <input type="submit" name="Submit3" value="Back">
  </p>
 
</form>
<p> </p>
</BODY>
</html>

Open in new window

0
 

Author Comment

by:saragose
ID: 23627548
Hey there,
The code that you gave me gives me the correct pop-up.
Unfortunately, if you look at the source code, it doesn't seem to be changed.
Is that normal? And will the next page be able to read this hidden box correctly using the request object?

Thanks
Eric
0
 
LVL 18

Accepted Solution

by:
Morcalavin earned 200 total points
ID: 23627640
Oh.  I see where you are coming from now.  You will not see the source code changed in any way.  Any changes you make to your html document via javascript are basically stored "in memory" of the browser.  However, this doesn't mean that is doesn't get 'saved' when you submit the form.  After you populate the hidden input via javascript, it contains that value until you reload the page or change the value again.  Hitting submit will submit the value you changed it to to the server.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month17 days, 11 hours left to enroll

829 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