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=???;

You could use the string .split() method which will split it in to an array, which you can then join with linebreaks (
or <br>)

e.g.:


var someString, theArray, theResult;

someString = "Item1, Item2, Item3";
theArray = someString.split(","); // ["Item1", " Item2", " Item3"]
theResult = theArray.join("<br>"); // "Item1<br>Item2<br>Item3"

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?

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:


<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:


var listElements, i, length;

listElements = document.getElementsByTagName("li");
length = listElements.length;

for (i = 0; i < length; i++) {
  //do something with listElements[i]
}

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”);

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.

        <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>

is just a shorter way of doing

var listElements;
var i;
var length;

As in initializing variables. Right?

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.

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