SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Formating Xmlhttp.responsetext

    xmlhttp.responseText = "Bob , Brittany , Brian"

    var txt=xmlhttp.responseText;
    var txt2 = txt.slice(0,txt.indexOf(","));

    slices off Bob.

    How do I get all three into a var with line breaks between them so I can display them with

    document.getElementsByTagName('body')[0].appendChild(div);
    document.getElementById('link_container').innerHTML=????????;

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    You could use the string .split() method which will split it in to an array, which you can then join with linebreaks (\n or <br>)

    e.g.:

    Code javascript:
    var someString, theArray, theResult;
     
    someString = "Item1, Item2, Item3";
    theArray = someString.split(","); // ["Item1", " Item2", " Item3"]
    theResult = theArray.join("<br>"); // "Item1<br>Item2<br>Item3"
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Perfect! Thank-you very much.

    I have another question. I haven't found a good explanation for the array index and how it works.

    I understand that [0] is the array index in:

    document.getElementsByTagName('body')[0].appendChild(div);

    Can you explain the array index and how it works, or provide a reference that you like?

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Because you're using getElementsByTagName() it returns an array of all tags that it finds, even if there is only 1 item, it will still be put in to an array. Some other methods like getElementsByClassName() work in the same manner.

    However, there are yet others that will return a single result element, for example getElementById().

    The way this is useful is if you wanted to iterate through a set of elements, e.g. let's say you have the following HTML:

    Code html4strict:
    <ul>
      <li>This is list item 1</li> 
      <li>This is list item 2</li> 
      <li>This is list item 3</li> 
    </ul>

    You can iterate through all the list elements like so:
    Code javascript:
    var listElements, i, length;
     
    listElements = document.getElementsByTagName("li");
    length = listElements.length;
     
    for (i = 0; i < length; i++) {
      //do something with listElements[i]
    }
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'm not familiar with this notation:

    var listElements, i, length;

    How's it interpreted?

    Isn't there a conflict with:

    listElements = document.getElementsByTagName("li");

  6. #6
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In addition to AussieJohn's explanation, document.getElementsByTagName() gets all the elements in the entire document with the specified tag name.

    But say you have 2 <ul>'s and you want to get the <li>s for just one of them, say the <li>s in ul2.

    Code:
            <ul id="ul1">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            
            <ul id="ul2">
                <li></li>
                <li></li>
            </ul>
    
         <script type="text/javascript">
            
             var ul2LiO = document.getElementById('ul2').getElementsByTagName('li');
        
             alert(ul2LiO.length);  //outputs 2
        
        </script>

  7. #7
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nichemtktg View Post
    I'm not familiar with this notation:

    var listElements, i, length;
    is just a shorter way of doing

    Code:
    var listElements;
    var i;
    var length;

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As in initializing variables. Right?

  9. #9
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, that's right.

    In AJ's case he was initialising 3 vars all on the 1 line instead of separate lines. You separate the variables names with a comma.

    And you don't have to assign the variable a value initially.

    Think of your RAM as a huge pile of pidgeon holes that can store data as variables and their values.

    When you type var myVar; without giving myVar an initial value, what you are doing is giving one of the pidgeon holes in your RAM a name myVar but not actually putting a value in the pidgeon hole. So initially it will be set to null until you assign a value to myVar elsewhere in your code.

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help this evening AussieJohn, and webdev1958. You've made this a quick and extremely informative topic.

    It's almost 10 pm here and I've been up since 5am. Time to feed the animals and hit the rack.

    Thanks Again,

    Niche


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
  •