How to use a function to define a hidden input field

R

Ron Aronica

I want to specify the value of an input field using the return from a
function. I want to do something this this:

<script>
function value() {
return "the value";
}
</script>

<form>
<input name="theValue" type="hidden", value=value()>
</form>

but it does not work. How do I accomplish this?

Thanks
 
E

Erwin Moller

Ron said:
I want to specify the value of an input field using the return from a
function. I want to do something this this:

<script>
function value() {
return "the value";
}
</script>

<form>
<input name="theValue" type="hidden", value=value()>
</form>

but it does not work. How do I accomplish this?

Thanks

Hi,

Just set it like you set any other field:
before i show you how, you should first use good syntax.

1) Name your form, so you can adres it!
2) <input name="theValue" type="hidden", value=value()>
is bad.
It should be:
<input name="theValue" type="hidden", value="someValueHere">

3) better use the following scripttag:
<script type="text/javascript">


Now you can do the following:

I added a button to fire an event which I use to set your value to the
hidden field. Of course I don't know how you will use it, this is just used
to call the function setHiddenValue().


-------------------------------------
<script type="text/javascript">
function value() {
return "the value";
}

function setHiddenValue(){
hiddenvalue=value();
document.forms["myForm"].theValue.value=hiddenvalue;
}

</script>

<form name="myForm">
<input name="theValue" type="hidden" value="">
<input type="button" value="Click me to set hidden value"
onClick="setHiddenValue()">
</form>

-------------------------------------

I coded an extra function to show how things work.
You can also put the whole logic into 1 line.

setting a simple value always works like this:
document.forms["*formname*"].*formelementname*.value=*newvalue*;

Hope that helps you.

Regards,
Erwin Moller
 
R

Ron Aronica

Erwin said:
Ron Aronica wrote:

I want to specify the value of an input field using the return from a
function. I want to do something this this:

<script>
function value() {
return "the value";
}
</script>

<form>
<input name="theValue" type="hidden", value=value()>
</form>

but it does not work. How do I accomplish this?

Thanks


Hi,

Just set it like you set any other field:
before i show you how, you should first use good syntax.

1) Name your form, so you can adres it!
2) <input name="theValue" type="hidden", value=value()>
is bad.
It should be:
<input name="theValue" type="hidden", value="someValueHere">

3) better use the following scripttag:
<script type="text/javascript">


Now you can do the following:

I added a button to fire an event which I use to set your value to the
hidden field. Of course I don't know how you will use it, this is just used
to call the function setHiddenValue().


-------------------------------------
<script type="text/javascript">
function value() {
return "the value";
}

function setHiddenValue(){
hiddenvalue=value();
document.forms["myForm"].theValue.value=hiddenvalue;
}

</script>

<form name="myForm">
<input name="theValue" type="hidden" value="">
<input type="button" value="Click me to set hidden value"
onClick="setHiddenValue()">
</form>

-------------------------------------

I coded an extra function to show how things work.
You can also put the whole logic into 1 line.

setting a simple value always works like this:
document.forms["*formname*"].*formelementname*.value=*newvalue*;

Hope that helps you.

Regards,
Erwin Moller

Thanks Erwin, This is what I need to know. I agree with your formatting
comments. I was only trying to show the key items, not the complete code.

Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top