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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    dJogja id
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getAttribute with "this"

    help,

    i make function like this:

    function chrCount(which){
    maxChr = document.getElementById(which.name).getAttribute('maxlength');
    if (which.value.length > maxChr)
    which.value = which.value.substring( 0 , maxChr )
    else
    document.getElementById('chrLeft').innerHTML = maxChr-which.value.length + " chr(s)Left" ;
    }

    html code:

    <input name="name" type="text" maxlength="8" onKeyDown="chrCount(this)" onKeyUp="chrCount(this)">
    <div id="chrLeft" ></div>

    above code work in IE6, not in Mozilla, can anybody tell the right code ? running in IE6 and DOM browser only is find.
    Thanks

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    You are passing getElementById a name, not an id. Huge difference in terms of DOM.

    Anyway, since you're passing the this reference:
    maxChr = which.getAttribute('maxlength');

    Is much clearer, faster, and less haphazard.

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    dJogja id
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    u r right!!! Why was I so stupid!! thx a lots...


  •  

    Posting Permissions

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