Assigning a JS variabke with a value from an HTML text box

R

rex_mundi

Please forgive me but i am quite new to this game....

i have an html text box (which is part of a from) to which a value will
be entered by a user. i need to assign this value that has been entered
to a JS variable.

would the sxcripting be as simple as:

var nameOfVar;

nameOfVar = textBoxName;

many thanks in advance
 
R

rex_mundi

think i may have found it but just some clarification that i am heading
in the right direction would be helphul now thanks....

nameOfVariable = this.document.testForm.testString.value;

sorry my head really isn't working all that well at the minute.

thanks again

RM
 
M

Michael Winter

think i may have found it but just some clarification that i am heading
in the right direction would be helphul now thanks....

nameOfVariable = this.document.testForm.testString.value;

Close, but not quite:

myVar = document.forms.testForm.elements.testString.value;

Using the this operator, as you do above, isn't advisable in the general
case as the object it references changes. However, it can be put to good
use. For example, in event listeners, the this operator value is a
reference to the target element:

<form ... onsubmit="return validate(this);">

In the snippet above, the validate function would be called with a
reference to the form as its argument. Within the function, one can then
access controls quite simply:

function validate(form) {

form.elements.controlName.value

}

For more information about accessing form controls, see the FAQ
(<http://www.jibbering.com/faq/#FAQ4_13>).

Mike
 
R

rex_mundi

Mike,

Thanks for the help....

Reason behind using the operator is that is is used in another function
and then reset before it is used again.

So the change in data is not a problem.

Many thanks

RM
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top