Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getElementById and NetScape problem

    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?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    try this
    Code:
    <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

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    168
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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>

  • #4
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hummmm
    Thanks, It works know.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    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.

    Code:
    <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.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    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.

    Code:
    <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.

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    language is deprecated, it should be
    Code:
    <script type="text/javascript">
    function clicktest(oForm)
    {
      alert(oForm.myTextBox.value);
      alert(oForm.myTextBox2.value);
    }
    </script>

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Sorry, it was a typo.
    Follow what _Aerospace_Eng_ said.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •