Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. getElementByID with array not finding string

getElementByID with array not finding string

Scheduled Pinned Locked Moved Web Development
htmldata-structureshelpquestionannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xaphod
    wrote on last edited by
    #1

    Hi, The array(0) is foo when I type it out trough DOM so why cant this work? Ive tried a lot, please help! [code] function handleResponse() { if(http.readyState == 4){ var response = http.responseText; var update = new Array(); if(response.indexOf('|' != -1)) { update = response.split('|'); document.getElementById(update[0]).innerHTML = update[1]; //not working! //document.getElementById('foo').innerHTML = update[1];//works! } } } [/code]

    G 1 Reply Last reply
    0
    • X xaphod

      Hi, The array(0) is foo when I type it out trough DOM so why cant this work? Ive tried a lot, please help! [code] function handleResponse() { if(http.readyState == 4){ var response = http.responseText; var update = new Array(); if(response.indexOf('|' != -1)) { update = response.split('|'); document.getElementById(update[0]).innerHTML = update[1]; //not working! //document.getElementById('foo').innerHTML = update[1];//works! } } } [/code]

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Why are you creating an array that you don't use? response.indexOf('|' != -1) should be response.indexOf('|') != -1 --- b { font-weight: normal; }

      X 1 Reply Last reply
      0
      • G Guffa

        Why are you creating an array that you don't use? response.indexOf('|' != -1) should be response.indexOf('|') != -1 --- b { font-weight: normal; }

        X Offline
        X Offline
        xaphod
        wrote on last edited by
        #3

        Hi Guffa, Im getting this XmlHttpResonse "foo|This is foo". That was my intention anyway. Turns out I'm getting a lot of other stuff to so that was my problem, array(0).length was 250 not 3 (foo). Btw, seems like there is no difference in those two coding techniques for indexOf, they both work, but yours is more good looking! Thanks! /x

        G 1 Reply Last reply
        0
        • X xaphod

          Hi Guffa, Im getting this XmlHttpResonse "foo|This is foo". That was my intention anyway. Turns out I'm getting a lot of other stuff to so that was my problem, array(0).length was 250 not 3 (foo). Btw, seems like there is no difference in those two coding techniques for indexOf, they both work, but yours is more good looking! Thanks! /x

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          xaphod wrote: Btw, seems like there is no difference in those two coding techniques for indexOf, they both work, but yours is more good looking! Are you kidding? My code works, the other doesn't. The expression '|' != -1 will always evalute to the value true, as the string '|' always will be different from the number -1. As the indexOf method uses a string parameter, the boolean value true will be converted to the string 'true'. The call response.indexOf('|' != -1) therefore does exactly the same as response.indexOf('true'). Unless the string response starts with the letters 'true', the call will always return a non-zero value. --- b { font-weight: normal; }

          X 1 Reply Last reply
          0
          • G Guffa

            xaphod wrote: Btw, seems like there is no difference in those two coding techniques for indexOf, they both work, but yours is more good looking! Are you kidding? My code works, the other doesn't. The expression '|' != -1 will always evalute to the value true, as the string '|' always will be different from the number -1. As the indexOf method uses a string parameter, the boolean value true will be converted to the string 'true'. The call response.indexOf('|' != -1) therefore does exactly the same as response.indexOf('true'). Unless the string response starts with the letters 'true', the call will always return a non-zero value. --- b { font-weight: normal; }

            X Offline
            X Offline
            xaphod
            wrote on last edited by
            #5

            You're right! Thanks for sharing! Best regards /xaphod

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups