PDA

View Full Version : storing the value of a hidden field in a variable


tater
12-06-2003, 04:13 PM
hiddenFieldName is assigned the value of 1 in the body

code.....
var hiddenFieldValue = document.someForm.hiddenFieldName.value;
document.write(hiddenFieldValue.length);

the result is 6

How can a value = 1 and be 6 chars long?

I tried using hiddenFieldValue.toString() and it produced the same results.
I tried using hiddenFieldValue.charAt(0) and it still said it was 6 chars long.

When I do the following

code.....
hiddenFieldValue = "type" + hiddenFieldValue;
document.write(hiddenFieldValue);

It produces type 1

Why doesn't it yield type1 with no space and why is lineItem 6 chars long?

Any ideas??? Thanks

glenngv
12-06-2003, 04:43 PM
Show us the whole code

tater
12-06-2003, 04:46 PM
are you sure it is 394 lines long

all i am wanting to solve is how to get the value of a hidden form element stored in a variable within a function

liorean
12-06-2003, 04:47 PM
You could take out the unrelated parts. Or just hand us a link.

adios
12-06-2003, 05:21 PM
...soon you'll have 394 posts on the same subject. :rolleyes:

http://www.codingforums.com/showthread.php?threadid=29807
http://www.codingforums.com/showthread.php?threadid=29819
http://www.codingforums.com/showthread.php?threadid=29825
http://www.codingforums.com/showthread.php?threadid=29839

tater
12-06-2003, 05:26 PM
ok take a look

I need to do the same thing as in function newCompanyOption
for brand, model, and type the only difference is that according to the number of lines on the PO there will be that many of each

In other words there will be brand1, brand2, brand3 if itemCount is 3

I only want to set the new option to selected for the last one. In this case it would be brand3

The only way from within the script to know which brand to change is by accessing itemCount

Inthe code as written if itemCount is equal to 1, itemField in function newTypeOption is type 1 and is 10 chars long. This is what I don't understand.


newPo.php has the code as referenced above.
newType.php has a function that calls newTypeOption in newPo.php

Thanks and I hope this code is not worst you've ever seen

tater
12-06-2003, 07:15 PM
please take a look at the code below this is where my problem lies when the test button on this program is clicked the output is as follows:
string 1
10

Why is it not:
string1
7

<html>
<head>
<title>Untitled Document</title>
</head>
<script>
function addValueOfHiddenFieldToString()
{
var hiddenFieldValue = document.newForm.hiddenField.value, hiddenFieldValueString, newString;
newString = 'string' + hiddenFieldValue;
document.write(newString+'<br>'+newString.length);
}
</script>
<body>
<form name="newForm" method="get">
<input type="button" value="test" onClick="addValueOfHiddenFieldToString()">
<input type="hidden" name="hiddenField" value="
<?php
$hiddenField = 1;
echo $hiddenField;
?>">
</form>
</body>
</html>

if the hiddenField line reads
<input type="hidden" name="hiddenField" value="1">
the ouput is
string1
7


Thanks

liorean
12-06-2003, 10:54 PM
Hmm, I would guess the php or the newline you put before it somehow inserts space before and/or after the '1'. Trimming that space away should be easy - before you add 'string' in front of it, use a function that does something like this:function fnTrim(sIn){
return sIn.replace(/^\s+/,'').replace(/\s+$/,'');
}

tater
12-06-2003, 11:17 PM
Thanks coding master the child is humbled by your knowledge and needs to learn more about RegExp. I will consult O'Reilly for further knowledge on this matter.

liorean
12-06-2003, 11:22 PM
If you need to know more about regex, look no further than my signature. I think the article is fairly complete when it comes to regex in JavaScript, the only part I've found missing is the more advanced syntaces for string.replace, which you on the other hand have prime information about on Devedge's JavaScript tutorial and references, which you will also find a link to in my sig (Moz: JavaScript)...