Click to See Complete Forum and Search --> : problem with assignment from hidden field to text field using eval
dcdalton
08-23-2003, 05:05 PM
Ive absolutely beaten my forehead bloddy on this one & just dont see what the heck I am doing wrong .... ALL Help would be worth its weight in gold! (Well not REAL gold). I will only bore you all with one field ... If I can get one going I can get the others. Anywho here is what Im doing:
I have hidden fields being added to my page with information about suppliers (there can be more than one) So Im adding fields like this:
<input type='hidden' name='30003_name' value='Some Name'>
then I have a form for the user to enter data IF they dont choose one of the suppliers. In that I have a field as such:
<input type='text' name='sup_name'>
If they do select one of the existing suppliers I am trying to load the data from the hidden fields into the form using the onChange even of a drop down that has the cages (that 30003 in the hidden field is one). Im getting into the function with NO problem at all and getting the proper code from the drop down BUT I CANNOT get the eval to work & load the data. I did it with a simple assignment of a string ... no problem. BUT everytime I get near this stupid eval it blows up. Here is the function:
function fillData(cageCode) {
with (document.forms[0]) {
if (cageCode != "") {
sup_name.value = eval(cageCode+"_name").value;
}
}
}
I have tried EVERY stinking version of this I can think of, been thru 4 books & 5 websites on eval. I have used eval many, many times with only minor aggravation but this one is just driving me NUTS! Thanks all in advance!
gil davis
08-23-2003, 05:13 PM
My opinion of the "eval" statement is that the "a" should be changed to an "i". So I avoid it like the plague...
sup_name.value = document.forms[0][cageCode + "_name"].value;
David Harrison
08-23-2003, 05:17 PM
If you were to change this line:
<input type='hidden' name='30003_name' value='Some Name'>
to this:
<input type='hidden' id='name_30003' value='Some Name'>
And also change all other name attributes into id attributes, (id attribute values should not start with a number by the way), you could do this in your script:
sup_name.value=eval("document.getElementById('name_'+"cageCode+").value");
dcdalton
08-23-2003, 06:24 PM
well I tried both ways .... still no good. This one:
sup_name.value = document.forms[0][cageCode + "_name"].value;
gives an error document.forms[0][...].value is null or not an object
This one:
sup_name.value=eval("document.getElementById('name_'+"cageCode+").value"); (After changing it to an ID as such ..<INPUT TYPE='hidden' id='name_30003' VALUE='NAVAL AIR SYSTEMS COM'>)
causes a javascript error the secoond the page is loaded at line 7 (object expected) Line 7 is the funtion definition. I commented out the assignment & error is gone. BTW, isnt that + before the "cageCode supposed to be "+cageCode ? Anyways I tried that too ...... nfg
So I made SURE my HTML was right .... sure enough just fine & then threw these 2 lines into the function by themselves:
alert("Supplier name in form is: "+sup_name.value);
alert("Hidden field for 30003 name is: "+name_30003.value);
and sure enough the field values come up in the alert just fine. So I know the html is right, the function CAN see the form elements but still this stupid eval just blows up everything!
I do agree with Gil ... eval is the WORST but in this case I can have quite a few manufacturers in the hidden fields so unfortunately I have to use it. Still scratching my head here!
dcdalton
08-23-2003, 06:45 PM
OK HOLD THE FORT ...... I just did a simple assignment in the function:
sup_name.value = eval("name_"+cageCode).value;
and by jove it worked! I can only hypothesize that with the number first eval was attempting to do some weird math & was really wreaking havok! Yikes
I do thank all for the input thought ... taking a break & eating seems to have cleared my head
David Harrison
08-24-2003, 10:17 AM
There was a slight mistake in what I posted, it should have been this:
sup_name.value=eval("document.getElementById('name_"+cageCode+"').value");
I don't know what both of you have against eval though.
Charles
08-24-2003, 10:25 AM
Originally posted by lavalamp
I don't know what both of you have against eval though. sup_name.value=eval("document.getElementById('name_"+cageCode+"').value");
resolves to
sup_name.value=document.getElementById('name_'+cageCode).value;
except that it makes the browser, and the web author, work harder. It's kind of like typing "1 + 1 + 1 + 1" instead of "4".
David Harrison
08-24-2003, 10:28 AM
I didn't think of that, but still there are times when eval is neccessary. So what's wrong with it? And if nothing is wrong with it what do dcdalton and gil davis have against it?
dcdalton
08-24-2003, 10:29 AM
Dont really have anything against eval except that it tends to have some quirky side effects sometimes..... probably just me but I just seem to have some weird fallout everytime I use it... once again probably just me
Charles
08-24-2003, 10:34 AM
Originally posted by lavalamp
I didn't think of that, but still there are times when eval is neccessary. So what's wrong with it? And if nothing is wrong with it what do dcdalton and gil davis have against it? It is very, very rare to need to use the "eval()" function. So rare that I doubt that you could give me an example of where it is necessary or even helpful. It's use almost always is an indication that the web author knows very little about JavaScript, or at least knows less than he or she thinks.
When guest in somebody's house you shouldn't make your host work harder than necessary and when your page is a guest in somebody's browser you shouldn't make their machine work harder than necessary.