Newbie question and problem

B

Bruce W.1

This is my first javascript effort and it's not going well so far.
Guess it's not so easy after all.

I want a text box to be populated with some text when I push a button.
Simple yes? It just isn't working for me though. I'm testing in
Dreamweaver's preview IE browser window.

The code is below. Can anyone tell me what's wrong? Thanks for your help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<script type="text/javascript">
function fill(form) {
form.SubmitButton.value = "hello world";
}
</script>
<body>
<form name="form1" method="post" action="">
<p>
<input type="text" name="textfield">
</p>
<p>
<input type="submit" name="SubmitButton" value="Submit"
onClick="fill(this.form1)">
</p>
<p>&nbsp;</p>
</form>
</body>
</html>
 
M

McKirahan

Bruce W.1 said:
This is my first javascript effort and it's not going well so far.
Guess it's not so easy after all.

I want a text box to be populated with some text when I push a button.
Simple yes? It just isn't working for me though. I'm testing in
Dreamweaver's preview IE browser window.

The code is below. Can anyone tell me what's wrong? Thanks for your help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<script type="text/javascript">
function fill(form) {
form.SubmitButton.value = "hello world";
}
</script>
<body>
<form name="form1" method="post" action="">
<p>
<input type="text" name="textfield">
</p>
<p>
<input type="submit" name="SubmitButton" value="Submit"
onClick="fill(this.form1)">
</p>
<p>&nbsp;</p>
</form>
</body>
</html>

You assigned the value to the button not to "textfield".

I stripped it down some; try this:

<html>
<head>
<title>noobie.htm</title>
</head>
<script type="text/javascript">
function fill() {
document.form1.textfield.value = "hello world";
}
</script>
<body>
<form action="" method="post" name="form1">
<input type="text" name="textfield">
<input type="button" value="Submit" onClick="fill()">
</form>
</body>
</html>
 
D

Dietmar Meier

Bruce said:
function fill(form) {
form.SubmitButton.value = "hello world";
}
[...]
<input type="submit" name="SubmitButton" value="Submit"
onClick="fill(this.form1)">

"this" in your event handler code references the button
input element, which has a property "form" to reference
the associated form element, but no property "form1",
which is just the form's name attribute's value.

Use ...

function fill(form) {
form.elements["SubmitButton"].value = "hello world";
}
[...]
<input type="submit" name="SubmitButton" value="Submit"
onClick="fill(this.form)">

.... or ...

function fill(button) {
button.value = "hello world";
}
[...]
<input type="submit" name="SubmitButton" value="Submit"
onClick="fill(this)">

.... or ...

<input type="submit" name="SubmitButton" value="Submit"
onClick="this.value='hello world'">

ciao, dhgm
 
B

Bruce W.1

Dietmar said:
"this" in your event handler code references the button
input element, which has a property "form" to reference
the associated form element, but no property "form1",
which is just the form's name attribute's value.

Use ...

function fill(form) {
form.elements["SubmitButton"].value = "hello world";
}
[...]
<input type="submit" name="SubmitButton" value="Submit"
onClick="fill(this.form)">

... or ...

function fill(button) {
button.value = "hello world";
}
[...]
<input type="submit" name="SubmitButton" value="Submit"
onClick="fill(this)">

... or ...

<input type="submit" name="SubmitButton" value="Submit"
onClick="this.value='hello world'">

ciao, dhgm
=================================================

Aha! It was my incorrect usage of 'this' that messed me up. I thought
'this' referred to the form and not the button. Thanks.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top