www.webdeveloper.com
Results 1 to 3 of 3

Thread: function error if trigger is inside a form

  1. #1
    Join Date
    Dec 2005
    Location
    Bucharest, Romania
    Posts
    21

    function error if trigger is inside a form

    Hello,

    I have the following javascript code:
    Code:
    <script type="text/javascript">
    function key(){
    	var text = document.getElementById("name");
    	var res1 = text.value.replace(/&/g, ",");
    	var res2 = res1.replace(/&amp;/g, ",");
    	var res3 = res2.replace(/ /g,",");
    	document.getElementById("key").value = res3;
    }
    </script>
    and
    HTML Code:
    <input id=name type=text name=name value="a&b">
    <input id=key type=text name=key value="">
    <input type='button' onclick="key()" value='check'>
    Everthing is ok unless I put the HTML inside <form> tags.

    So the above code is working but the below code:
    HTML Code:
    <form>
    <input id=name type=text name=name value="a&b">
    <input id=key type=text name=key value="">
    <input type='button' onclick="key()" value='check'> 
    </form>
    generates a "Object doesn't support this property or method" error.

    Why? What am I doing wrong and how can I correct it?

    Thank you

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You have both a function and an element name (and id) named the same: key. Change one of them.

    Within a form, elements may be referred straight upon their name, as they would have been Global variables. But functions are Global variables, as well. So, in your second attempt, you had two variables with the same scope and the same name, which is illegal.

  3. #3
    Join Date
    Dec 2005
    Location
    Bucharest, Romania
    Posts
    21

    Thumbs up

    Quote Originally Posted by Kor View Post
    You have both a function and an element name (and id) named the same: key. Change one of them.

    Within a form, elements may be referred straight upon their name, as they would have been Global variables. But functions are Global variables, as well. So, in your second attempt, you had two variables with the same scope and the same name, which is illegal.
    Thank you very much for the clear explanation.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles