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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    My rows are appearing horizontally! o.O

    The code so far works, but the rows form horizontally rather than vertically. This is my first time working with XMLHttpRequest() so it's probably pretty sloppy. Also, when the function runs again, it creates another row above the previous row, I'll add a screen shot.

    http://tinyurl.com/7c7fylb - What it looks like when I've typed "bloodb" which means everything after the return if statement has ran twice

    Code:
    function findCard()
    {
    str = document.getElementById("cardName").value;
    if (str.length<=4)
      {
      return;
      }
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","CardInfo/cards.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    var strLng = str.length;
    var x = xmlDoc.getElementsByTagName("card");
    var table = document.getElementById("cardDisplay")
    var newRow = table.insertRow(0)
    
    for (i=0; i  {
       var resaultStr = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue.slice(0,strLng)
       if (str==resaultStr)
       {
        newRow.insertCell(0).innerHTML=x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue
       }
    
       
      }
    
    }

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Ok, this is exactly what your code does (given that this is actually your code, you should know it)

    - newRow = table.insertRow(0) will create a new row on top of the table
    - newRow.insertCell will insert a cell into the current row. You are doing that inside the loop, so you will get a lot of cells in the same row

    So where is your problem? And what is the requirement?

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alright I got it to work

    The code actually would work, but since it's in a for loop I have to change the row index while it's in the for loop.
    What was confusing me was that out side of a loop, for instance if I just had a function with just

    newRow.insertCell(0).innerHTML=x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue

    code it would create a new row each time I ran the function. But inside a loop it just adds a cell. Still kinda strange to me.
    newRow is now set to 0
    table.insertRow(newRow).insertCell(0).innerHTML=x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue
    newRow=+1
    Last edited by Hobatron; 03-22-2012 at 12:58 PM.

  • #4
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    Good to hear you got your head around the problem!

    Just thought I'd post to say that, like may, I read the post title and thought 'Well, Rows are supposed to be horizontal!'

    Adios


  •  

    Posting Permissions

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