Text in textfield

W

Willy

Example
form>
<input name=userInput type=text value="something">
</form>
Could value'something' ibe put in a variable via avascript?
Sorry for previous message in dutch

Willy
 
T

Toby Inkster

Willy said:
<input name=userInput type=text value="something">
Could value'something' ibe put in a variable via avascript?

Yes.

<form name=theForm>
<input name=userInput type=text value="">
</form>
<script type="text/javascript">
var myInput = document.forms["theForm"].elements["userInput"];
myInput.value = "something";
</script>

Note: I've given the form a name here. You don't need to -- you could
refer to it by number (the first form on the page is 'document.forms[0]',
the next one is 'document.forms[1]', etc), but numbers can accidentally
change when you rearrange your page next month.
 
T

Toby Inkster

Toby said:
Willy said:
<input name=userInput type=text value="something">
Could value'something' ibe put in a variable via avascript?

<form name=theForm>
<input name=userInput type=text value="">
</form>
<script type="text/javascript">
var myInput = document.forms["theForm"].elements["userInput"];
myInput.value = "something";
</script>

Sorry -- I think I misread your question. My example above uses Javascript
to put something into a form field. It seems you want to read it *out*.

Here you have two options, depending on what you want to read. Say you
have '<input value="something">' but the visitor then types into the form
field 'something else'. Do you want to retrieve the original value
'something', or the new value 'something else'?

If you want to retrieve 'something else' (which most people would probably
want), then:

<form name=theForm>
<input name=userInput type=text value="something">
</form>
<script type="text/javascript">
var myInput = document.forms["theForm"].elements["userInput"];
var WhatIWant = myInput.value;
// now the variable 'WhatIWant' has what you want.
</script>

If you want to retrieve the original value ('something') then you need
'myInput.defaultValue' instead of 'myInput.value'.
 
W

Willy

Toby Inkster said:
Toby said:
Willy said:
<input name=userInput type=text value="something">
Could value'something' ibe put in a variable via avascript?

<form name=theForm>
<input name=userInput type=text value="">
</form>
<script type="text/javascript">
var myInput = document.forms["theForm"].elements["userInput"];
myInput.value = "something";
</script>

Sorry -- I think I misread your question. My example above uses Javascript
to put something into a form field. It seems you want to read it *out*.

Here you have two options, depending on what you want to read. Say you
have '<input value="something">' but the visitor then types into the form
field 'something else'. Do you want to retrieve the original value
'something', or the new value 'something else'?

If you want to retrieve 'something else' (which most people would probably
want), then:

<form name=theForm>
<input name=userInput type=text value="something">
</form>
<script type="text/javascript">
var myInput = document.forms["theForm"].elements["userInput"];
var WhatIWant = myInput.value;
// now the variable 'WhatIWant' has what you want.
</script>

If you want to retrieve the original value ('something') then you need
'myInput.defaultValue' instead of 'myInput.value'.

It is as you guessed the value 'something else' that I wanted to retrieve.
My regards and thank you for your help
Willy
 
S

Stan McCann

Example
form>
<input name=userInput type=text value="something">
</form>
Could value'something' ibe put in a variable via avascript?
Sorry for previous message in dutch

Yes, or server side. I use PHP and do the folloeing quite regularly.
Note the quotes and required block element.

<form id="form_id">
<legend>Something</legend><fieldset id="something">
<input name="userInput" type="text"
value="<?php echo $variable ?>">
</fieldset>
</form>
 
S

Stan McCann

Sorry -- I think I misread your question. My example above uses
Javascript to put something into a form field. It seems you want to
read it *out*.

If you did, so did I.
 

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,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top