SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    IE reporting wrong string length

    This is quite mysterious. I have several list items like this:
    HTML Code:
    <li>Private</li>
    I am looping through them all trying to get the text inside each list item for a comparison with a string from elsewhere.
    Code Javascript:
    var coresubmenu = document.getElementById('core').getElementsByTagName('li');
    for (var i = 0; i < coresubmenu.length; i++) {
      coresubmenu[i].onclick = function() {
        if (this.firstChild.nodeValue == 'Private') {
          // fail
        }
        alert(this.firstChild.nodeValue.length); // alerts 8
      }
    }
    IE alerts one more than the actual length in all cases. Google hasn't brought anything up for me on this one - any ideas?

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    This is quite mysterious. I have several list items like this:
    HTML Code:
    <li>Private</li>
    I am looping through them all trying to get the text inside each list item for a comparison with a string from elsewhere.
    Code Javascript:
    var coresubmenu = document.getElementById('core').getElementsByTagName('li');
    for (var i = 0; i < coresubmenu.length; i++) {
      coresubmenu[i].onclick = function() {
        if (this.firstChild.nodeValue == 'Private') {
          // fail
        }
        alert(this.firstChild.nodeValue.length); // alerts 8
      }
    }
    IE alerts one more than the actual length in all cases. Google hasn't brought anything up for me on this one - any ideas?
    Run this and you'll see why - it appends a space.
    Code:
    <script type='text/javascript'>
    var coresubmenu = document.getElementById('core').getElementsByTagName('li');
    
    for (var i = 0; i < coresubmenu.length; i++) {
      coresubmenu[i].onclick = function() {
        if (this.firstChild.nodeValue == 'Private') {
          // fail
        }
        alert(this.firstChild.nodeValue.length+'\n\n'+this.firstChild.nodeValue.charCodeAt(this.firstChild.nodeValue.length-1));
      }
    }
    </script>
    Tab-indentation is a crime against humanity.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Should have thought of doing that myself. Is this a well-known idiocy of IE's? I'd never encountered it before (obviously).

    Time to write me a trim() function.


Bookmarks

Posting Permissions

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