www.webdeveloper.com
Results 1 to 3 of 3

Thread: My rows are appearing horizontally :-/

  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Question My rows are appearing horizontally :-/

    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 so if there's a better way to do something, I'm all ears



    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<x.length;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
    }


    }

    }

    My table is just a blank table created in the body looks something like this

    <table id="cardDisplay" border="1">

    </table>

  2. #2
    Join Date
    Mar 2012
    Posts
    2
    As of now what I think is happening is, the code is just inserting new cells into the row. Which isn't what it should be doing... I think it's doing this because it's in a for loop. For instance if I just had a function with the

    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

  3. #3
    Join Date
    Nov 2007
    Posts
    418
    Your code currently creates a new row, then loops through the response and adds a cell to the new row during each loop.

    If you want each iteration of the loop to create a new row, you need to move "insertRow" into the loop.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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