how to write words into a div from a text input?

O

Offm

I was trying to make some experiences with javascript ,
making a div response to a text input but cannot say what am doing bad so if
anyone could help with the script,
many Thanks
the script follows
<script type="text/javascript">
function I(form)
{
if(form.e2.value=="")
document.getElementById("dc").innerHTML="something";
}
</script>
<p >
<form name="form">
<input name="e2" size="20" type="text">
<select name="e1">
<option>option.....
<option>option1
</select>
&nbsp;
<input src="" onclick="I(form)" align="right" type="image" width="60"
height="49"></p>
</form>
<p>&nbsp;</p>
<div id="dc">
</div>
 
E

Erwin Moller

Offm said:
I was trying to make some experiences with javascript ,
making a div response to a text input but cannot say what am doing bad so
if anyone could help with the script,
many Thanks
the script follows
<script type="text/javascript">
function I(form)
{
if(form.e2.value=="")
document.getElementById("dc").innerHTML="something";
}
</script>
<p >
<form name="form">
<input name="e2" size="20" type="text">
<select name="e1">
<option>option.....
<option>option1
</select>
&nbsp;
<input src="" onclick="I(form)" align="right" type="image" width="60"
height="49"></p>

Hi, You are sending form to the function.

To debug your code, simple alert your parameters and see what JavaScript say
they are.
In your case:
<script type="text/javascript">
alert ("Received form. form="+form);
function I(form)
{
if(form.e2.value=="")
document.getElementById("dc").innerHTML="something";
}
</script>

It will probably yell something like 'undefined'.

To fix your script:
1) Make a valid reference to the form in your function
like:
var myForm = document.forms["form"];

or

2) Pass a valid reference to the function from the onClick-handler.
You can use the keyword 'this', but it will reference to the image, not the
form.
onClick='I(this);';

I would advise 1), and when you get a hang of it, you can start using
'this'. (I never use this for such purposes, and always build the reference
inside the function, but that is personal taste and total imcompetence on
my side.)

Regards,
Erwin Moller
 
E

Erwin Moller

Erwin Moller wrote:

typo correction: The alert should be in the function of course.

<script type="text/javascript">
function I(form) {
alert ("Received form. form="+form);
if(form.e2.value==""){
document.getElementById("dc").innerHTML="something";
}
}
</script>

Regards,
Erwin Moller
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top