Click to See Complete Forum and Search --> : problem on variables
damon2003
12-05-2003, 10:36 AM
Hi,
I have a variable that alters to different values to match the names of different hidden fields -
var hiddenProductName="hiddenProductName"+k;
I want to put this in the following statement:
document.form1.hiddenProductName.value;
However, it is not working because the statement is taking 'hiddenProductName' literally, its not using the value I am storing in it. Does anyone know how to get around this?
thanks
requestcode
12-05-2003, 10:44 AM
This should work:
document.form1.hiddenProductName.value=hiddenProductName
If we could see the rest of your code we might be able to see if there is anything else causing the problem.
pixelmech
12-05-2003, 10:45 AM
Damon,
I believe what you want is:
var hiddenProductNameValue="hiddenProductName"+k;
document.forms['form1'].hiddenProductName.value = hiddenProductNameValue;
You have to assign the new value to your hidden input. Also using variables so closely named isn't helping (thus the change on the first). Assuming I got your question correctly...
HTH
Tom
damon2003
12-05-2003, 10:51 AM
Hi, here the code, its a bit messy, I have been trying different things.
I tried what you suggested , doesnt seem to be working at the moment though
//set up a variable to hold number of hidden inputs.
var k = 1;
var x = 0;
//get all input tags
var i = document.getElementsByTagName("INPUT");
//loop through all input tags
var hiddenProductName="";
var hiddenProductValue="";
var total="";
var productDescription="";
var checkName;
var checked="not checked";
for (var j = 0; j < i.length; j++)
{
//find hidden inputs
if (i.item(j).getAttribute("type") == 'checkbox') {
//if input is a checkbox, add to x
if (i.item(j).checked==true)
{
//Generate hiddenfield names
hiddenProductName="hiddenProductName"+k;
//var hiddenProductName=document.form1.hiddenProductName.value;
//hiddenProductValue=document.form1.'hiddenProductName'.value;
document.form1.hiddenProductName.value = hiddenProductNameValue;
//alert(hiddenProductValue);
attribute = i.item(j).getAttribute('name');
total=total+attribute;
//productDescription=productDescription+hiddenProductValue;
checked="checked";
}
x++;
k++;
}
} alert("Please check your order:\n");
pixelmech
12-05-2003, 10:58 AM
Originally posted by damon2003
hiddenProductName="hiddenProductName"+k;
document.form1.hiddenProductName.value = hiddenProductNameValue;
Check your variable names, they aren't correct.
Tom
damon2003
12-05-2003, 11:05 AM
LIke this? Still havnt got it working right?
hiddenProductName="hiddenProductName"+k;
document.form1.hiddenProductName.value = hiddenProductValue;
alert(hiddenProductValue);
pixelmech
12-05-2003, 11:14 AM
Originally posted by damon2003
LIke this? Still havnt got it working right?
hiddenProductName="hiddenProductName"+k;
document.form1.hiddenProductName.value = hiddenProductValue;
alert(hiddenProductValue);
You are setting a value with
hiddenProductName="hiddenProductName"+k;
but you are assigning
document.form1.hiddenProductName.value = hiddenProductValue;
Your variable names need to match for it to work. Always be very careful with variable names.
Tom
damon2003
12-05-2003, 11:35 AM
wait a minute, is this setting the value of the actual hidden field to a new value or pulling the value of the hidden field and storing it in a variable?
thanks a lot
pixelmech
12-05-2003, 12:54 PM
Originally posted by damon2003
wait a minute, is this setting the value of the actual hidden field to a new value or pulling the value of the hidden field and storing it in a variable?
thanks a lot
I mixed my terms up but if I understand you correctly, you want to assign a value to your hidden field. Forget what they are called.
Your variable that you will use is appending the value of K to it. So first you set that, we'll call it 'a'
var a = 'hiddenproductname' + k;
then you want to set a, being your new value, to be the value of the hidden field:
document.form['form1'].yourHiddenInput.value = a;
This is how you do it. Just rewrite it, naming things correctly and it should work fine.
Tom
ray326
12-05-2003, 02:47 PM
Originally posted by damon2003
Hi,
I have a variable that alters to different values to match the names of different hidden fields -
var hiddenProductName="hiddenProductName"+k;
I want to put this in the following statement:
document.form1.hiddenProductName.value;
However, it is not working because the statement is taking 'hiddenProductName' literally, its not using the value I am storing in it. Does anyone know how to get around this?
thanks
If I understand what you're trying to do then you may have to express that object as a string and then use eval() to get to it. This is just off the top of my head -- no testing involved:
o = eval("document.form1."+hiddenProductName)
o.value = newvalue;
pixelmech
12-05-2003, 03:00 PM
Originally posted by ray326
If I understand what you're trying to do then you may have to express that object as a string and then use eval() to get to it. This is just off the top of my head -- no testing involved:
o = eval("document.form1."+hiddenProductName)
o.value = newvalue;
No no - please don't foist eval() on people, its not needed. Read this about eval()
http://www.doxdesk.com/personal/posts/wd/20011010-eval.html
Secondly, it's simply a clean coding matter, which I am trying to get him to realize. Clean and organized coding is important. Because he hasn't kept his variable names straight, things aren't working. It's as simple as that.
Tom
ray326
12-05-2003, 04:12 PM
Originally posted by pixelmech
No no - please don't foist eval() on people, its not needed. Read this about eval()
http://www.doxdesk.com/personal/posts/wd/20011010-eval.html
Secondly, it's simply a clean coding matter, which I am trying to get him to realize. Clean and organized coding is important. Because he hasn't kept his variable names straight, things aren't working. It's as simple as that.
Tom
Sorry, but most of that page is crap and right now what you're telling him to do is not what it looks to me he is saying he NEEDS to do. Until he comes back and clarifies his requirement then the point is moot anyway.
damon2003
12-06-2003, 07:50 AM
I am actually trying to get the value FROM the hidden field, not set the hidden field to a new value.
the hidden fields are dynamically created so I have to dynamically create the names to reference the hidden fields
ray326
12-06-2003, 05:36 PM
Originally posted by damon2003
I am actually trying to get the value FROM the hidden field, not set the hidden field to a new value.
the hidden fields are dynamically created so I have to dynamically create the names to reference the hidden fields
Tom may have a better way of doing this but I still think you're going to have to go the eval() route due to the dynamic nature of the names. A quick test somewhere in your code might look like:
alert(eval("document.form1."+hiddenProductName+".value"))
or you might try
alert(document.form1[hiddenProductName].value)
As I said before, these are untested guesses.
damon2003
12-07-2003, 11:06 AM
alert(eval("document.form1."+hiddenProductName+".value"))
works perfectly
thanks a lot
damon2003
12-10-2003, 11:22 AM
hi,
now I need to SET the value of hidden fields dynamically
I tired this
document.form1.[hiddenTextfieldNewName].value = newmaxchars;
and this:
document.forms['form1'].hiddenTextfieldNewName.value = newmaxchars;
and this:
eval("document.form1."+hiddenTextfieldNewName+".value") = newmaxchars;
how can I get dynamic name assigned to a hidden field?
thanks a lot
ray326
12-10-2003, 03:33 PM
Does an input with the name hiddenTextfieldNewName already exist on the form? If not then you'll need to create it somehow.
Try this out. Frankly I'm amazed that it worked.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled</title>
<script language="Javascript">
function doit() {
e = document.createElement("input")
e.setAttribute("type", "text")
e.setAttribute("name", "blowfish")
e.setAttribute("value", "blowfish")
document.f.appendChild(e)
}
</script>
</head>
<body>
<form name="f">
<input type="text" name="hootie" value="hootie">
<input type="button" name="push" value="Push Me" onclick="doit()">
</form>
</body>
</html>
Now consider taking the value of the first input and using it for the name of the dynamically created input, passing that value as a parameter to doit().