...

View Full Version : getElementById and NetScape problem



mahmood
03-06-2005, 01:31 PM
I have no problem using getElementById on IE but I seem to have problem using it on Netscape.
this is the code:

<form name='form1'>
<input type='text' name='myTextBox'>
<input type='button' onclick='clicktest(this.form.name)'>
</form>

<script>
function clicktest(form_name)
{
alert(document.getElementById(form_name).myTextBox.value);
}
</script>

It works fine on IE but NetScape gives the following message:
Error: document.getElementById(form_name) has no properties

What is the problem?

_Aerospace_Eng_
03-06-2005, 01:38 PM
try this

<script>
function clicktest(form_name)
{
alert(document.getElementById(''+form_name+'').value);
}
</script>
<form name="form1">
<input type="text" id="myTextBox" name="myTextBox">
<input type="button" onclick="clicktest('myTextBox')">
</form>
single quotes are meant usually meant for javascript and double quotes are usually meant for html, likely reason it didn't work because there was no id called form1

Puffin the Erb
03-06-2005, 01:45 PM
You are using the 'name' attribute with getElementById. This gets elements by their Id value. Your form has not got an id value.
IE will infer that the name attribute value should also be the id value automatically, stricter browsers will not.

You could change the first part of your code to this:

<form id='form1'>
<input type='text' name='myTextBox'>
<input type='button' onclick='clicktest(this.form.id)'>
</form>

mahmood
03-06-2005, 01:51 PM
hummmm
Thanks, It works know. :thumbsup:

glenngv
03-07-2005, 01:07 PM
You're moving around when you already have the form reference inside the button and getting it again in the function. If you only want the form reference inside the function, then just pass this.form from the button.


<form name="form1">
<input type="text" name="myTextBox" />
<input type="text" name="myTextBox2" />
<input type="button" onclick="clicktest(this.form)" />
</form>
<script type="javascript">
function clicktest(oForm)
{
alert(oForm.myTextBox.value);
alert(oForm.myTextBox2.value);
}
</script>

Notice the function won't ever need to know what the form name or id is.

mahmood
03-24-2005, 01:48 AM
You're moving around when you already have the form reference inside the button and getting it again in the function. If you only want the form reference inside the function, then just pass this.form from the button.


<form name="form1">
<input type="text" name="myTextBox" />
<input type="text" name="myTextBox2" />
<input type="button" onclick="clicktest(this.form)" />
</form>
<script type="javascript">
function clicktest(oForm)
{
alert(oForm.myTextBox.value);
alert(oForm.myTextBox2.value);
}
</script>

Notice the function won't ever need to know what the form name or id is.
Thanks
I just had to change "type" to language.

_Aerospace_Eng_
03-24-2005, 02:13 AM
language is deprecated, it should be

<script type="text/javascript">
function clicktest(oForm)
{
alert(oForm.myTextBox.value);
alert(oForm.myTextBox2.value);
}
</script>

glenngv
03-28-2005, 03:39 AM
Sorry, it was a typo. :o
Follow what _Aerospace_Eng_ said.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum