0

I am trying out javascript for the first time and would like to know how to get elements from inside elements so that I could display them to the user. I was wondering if there is any javascript code that could go and fetch the following items:

<ul id="tests">
<li class="test w0">
    <div class="letter l0">a</div>
    <div class="letter l1">b</div>
    <div class="letter l2">c</div>
</li>
<li class="test w1">
    <div class="letter l0">a2</div>
    <div class="letter l1">b2</div>
    <div class="letter l2">c2</div>
</li>
<li class="test w3">
    <div class="letter l0">a3</div>
    <div class="letter l1">b3</div>
    <div class="letter l2">c3</div>
</li>
</ul>

I would like to get the letters inside of each list item that is inside of the "test" element. How would I do so using solely javascript?

All I want is to put the strings into an array to display to the user. I know how to display the info to the user, just not how to get the strings inside of the elements.

OUTPUT CODE:

 [a,b,c,a2,b2,c2,a3,b3,c3] //Store strings in array

I don't understand JQuery, so please I would just like a javascript method....

4 Answers 4

3

You can use querySelectorAll:

var ary = [];
var lis = document.querySelectorAll('li.test div.letter');
for (var i = 0; i < lis.length; i++) {
    ary[i] = lis[i].innerHTML;
}
console.log(ary); // outputs ["a", "b", "c", "a2", "b2", "c2", "a3", "b3", "c3"] 

jsFiddle example

Sign up to request clarification or add additional context in comments.

4 Comments

The OP could also use var lis = document.getElementById('tests').getElementsByTagName('*'); to generate the list. The textContent / innerText property might be more suitable than innerHTML.
Yea.. textContent or innerText over innerHTML. Talk about subtle bugs that will take you a while to track down ;)
Greg, this is a very good vanilla javascript solution. Simple and lets the browser figure out the best way to find all the elements.
As far as I'm aware, innerText is not part of any standard, valid only on block level elements, and is a Microsoft concoction.
0

You can try this:

Using jQuery:

var values = [];
$('#tests').find('.letter').each(function(i, element) {
    values.push($(this).text());
});

Using just JavaScript:

var values = [], inputs = document.getElementsByClassName('letter');
inputs.forEach(function(element) { 
    values.push(element.innerHTML);
});

Comments

0

If you are using jQuery you can use the map function. It is in the context of the unordered list. Here is the code:

var $ul = $('#tests'),
stringArray;

stringArray = $.map($ul.find('div'), function(el, index){
    return el.innerHTML;
});

console.log(stringArray);
$ul.after(stringArray);

JsFiddle: http://jsfiddle.net/8xkK8/

Comments

0

Pure Javascript solution that walks the DOM and gets just the text nodes:

  // Get just the text out of an element.  Strips formatting.
  getText = function (el) {
    var ret;
    var txt = [],
      i = 0;

    if (!el) {
      ret = "";
    } else if (el.nodeType === 3) {
      // No problem if it's a text node
      ret = el.nodeValue;
    } else {
      // If there is more to it, then let's gather it all.
      while (el.childNodes[i]) {
        txt[txt.length] = getText(el.childNodes[i]);
        i++;
      }
      // return the array as a string
      ret = txt.join("");
    }
    return ret;
  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.