473,480 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

JS changes hidden value, IE predicts future

3 New Member
Two years ago, member acoder gave patient help to a newbie whose function worked in IE but not in Firefox. I have a similar problem, but mine works fine in Firefox and exhibits astonishing behavior in IE.

Expand|Select|Wrap|Line Numbers
  1. function chooseList(x)
  2.     {    
  3.         alert(document.getElementById('myHiddenField').value);    
  4.         document.getElementById('myHiddenField').value=x;
  5.         alert(document.getElementById('myHiddenField').value);    
  6.         document.getElementById('myForm').submit();            
  7.     }
The function is a simple two-liner. I have added the alert boxes to help me see what is going on.

In Firefox, the following happens, which is exactly what I intended to happen:

The function is called by onChange in a Select box and a new value is passed to the function.
1. The first alert shows the current value of the hidden field.
2. The value of the hidden field is changed to the new value, x.
3. The second alert shows the new value.
4. The form is submitted, passing along the new value.

In IE7, the following happens:
1. The first alert shows x, the new value of the hidden field, although the line of code which changes the value has not yet run!
2. The value is presumably changed now. ?
3. The second alert shows the new value, and is the same as the first alert.
4. The form is submitted, but no value is passed on for the hidden field!!!

The hidden field is:
Expand|Select|Wrap|Line Numbers
  1. <INPUT ID="myHiddenField"  NAME="myFieldName"  TYPE="Hidden" VALUE=1>

So there are two problems here. I would be grateful for any help in getting IE7 to behave when carrying out this function. Remember: it all works flawlessly in Firefox 3. Thank you!
BTW, the thread is showing spaces in the word 'value' in my function. These spaces are not in my code, not showing in the preview window and I cannot edit them out. (Sorry for not using code tags!)
Frank
Apr 11 '09 #1
6 3583
mrhoo
428 Contributor
Is it possible that you do not have values defined for the options of the select element?

I mention this because in the absence of value attributes, browsers other than IE default to using the text content of the selected option as the value, and IE does not, in scripts. They all submit the text if there is no value, however.
Apr 12 '09 #2
FrankF
3 New Member
Thank you for your response! Following the direction you suggest, I have checked as to how the variable is being assigned to the hidden field. I discovered that the variable is being passed to the function as a string. This does not bother Firefox, but maybe IE cannot handle it.

Expand|Select|Wrap|Line Numbers
  1. function chooseList(x)
  2.     {    
  3.                 alert(typeof(x));
  4.         x= parseInt(x);
  5.         alert(typeof(x));
  6.         document.getElementById('myHiddenField').value=x;
  7.         var qwerty = document.getElementById('myHiddenField').value;
  8.         alert(typeof(qwerty));
  9.     }
The variable x is an integer passed as a ColdFusion variable to the function. The first alert tells me that x is now a string, so in the next line of the function I convert it to a number. The second alert shows "number". Then I assign that number as the value of the hidden field. When the third alert pops up, it shows that the number has been converted back to a string! When the hidden value is passed on to the next page, in Firefox the value is passed on. In IE it reverts to the default value of zero, because for some reason it is not being passed on. I will keep plugging away at this one!
Apr 12 '09 #3
acoder
16,027 Recognized Expert Moderator MVP
The value is always a string even if you set it to an integer.

Show the HTML code for the select element.
Apr 13 '09 #4
FrankF
3 New Member
I discovered the cause of the problem. My Select box had no ID, but its Name was the same as the ID of the Hidden Field. Careless coding on my part.

Firefox and IE handle the situation differently. IE was allowing the Javascript "getElementById" to change the value of the select box even though it didn't have an ID attribute. Then it did not change the value of the following hidden field because it had already changed a control with the same Name as the hidden field's ID.

Firefox changed the values of both the Select box and the hidden field, and thus covered up the damage done by my code. Problem solved after several hours of debugging, including changing the form method to Get, after which I could see exactly what data was being passed.
Apr 15 '09 #5
Dormilich
8,658 Recognized Expert Moderator Expert
info: FireBug (Firefox extension) is able to show you the complete HTTP request (including POST data)
Apr 15 '09 #6
acoder
16,027 Recognized Expert Moderator MVP
@FrankF
It's an IE bug. document.getElementById() should only get an element by its ID, not its name.
Apr 16 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1388
by: Simon Harvey | last post by:
Hi all, Is there any easy way to check a field for calues that have changed on a post back. So the page is sent to the user, the user changes some values and I need to know which ones...
12
7421
by: Alan Silver | last post by:
Hello, I have a page that gets passed an ID in the query string. It then uses this ID to pull info out of a database and populate controls on the page. When the page is posted back, the query...
19
10516
by: k.karthikit | last post by:
Hello all, In some hidden variable (<input type="hidden" name="hiddenId" value="test" /> ,i stored some value.I accessed the value "test" using var id = document.getElementById( 'hiddenId' );...
5
2740
by: dvwool | last post by:
Hello, Another newbie here... I've been trying to make this work for days now and have finally decided to post as I can't seem to get it to work. Here's what I'm trying to do... I have a...
14
2225
by: Robert S | last post by:
I am trying to use POST to transfer data to another page. When I do this, '.' characters get converted to"_". For example: #index.html: <form action="test.php" method="post"> <input...
4
45817
by: Bosconian | last post by:
I've been fighting with this for an hour. My form contains a hidden input with the value initially set to "". When a user clicks on the link, a function is called that updates the hidden form...
1
7034
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form...
3
3486
by: dba | last post by:
Without giving a lot of script at this time, I am having a problem with php and form input type="hidden". Problem is that the hidden variable displays last line from database query. Some code....
5
4223
by: laredotornado | last post by:
Hi, Is there a way to trigger an event when the value of a hidden field changes? At a certain point in time my hidden field is getting changed to a value that I don't want but I can't find a...
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.