0

What I need to get all value of multiple li from the HTML code.

<div class="row">
<div style="margin-left:11px">
    <strong>Detail Product</strong>
</div>
    <div class="col-sm-12">
        <ul class="item-highlights">
            <li>4G LTE</li>
            <li>Dual Sim</li>
            <li>RAM 1GB</li>
        </ul>
    </div>
    <div class="col-sm-12">
        <ul class="item-highlights">
            <li>ROM 8GB</li>
            <li>Screen 5.5</li>
            <li>Warranty 1 Year</li>
        </ul>
    </div>

This how i get with javascript:

var test = document.getElementById('block-system-main').getElementsByClassName('item-highlights item-highlights')[0].innerHTML;

and i get the answer:

<li>4G LTE</li><li>Dual Sim</li><li>RAM 1GB</li>
2
  • 1. Where is block-system-main? 2. Why same class used twice in getElementsByClassName('item-highlights item-highlights') 3. To get the innerHTML, iterate over the HTMLCollection and get the innerHTML of individual element. Commented Mar 3, 2016 at 3:49
  • 1. i'm sorry i forget to put it, block-system-main is id on <section> 2. I thought i can access 2 class. can you help me to correct the code? Commented Mar 3, 2016 at 3:57

5 Answers 5

1

Heres an easy to understand answer.

var items = document.querySelectorAll( ".item-highlights li");
var values = [];
for( var n = 0; n < items.length; n++)
    values.push( items[n].innerHTML);

If you know css then its simple to change the call to "querySelectorAll" as it is only comparing things through the same way css does, So you can change it however you like.

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

2 Comments

The output answer is 6, i dont want to count the li tag.
What do you mean? Thats not what this is doing. The "Values" variable will contain an array of strings, each one being the list items' values
1

Expanding on @Tushar comment:

var test = '';
[].forEach.call( document.querySelectorAll('#block-system-main .item-highlights'), function(item) { return test += item.innerText; })

Check demo - Fiddle.

5 Comments

Better selector: document.querySelectorAll('#block-system-main .item-highlights')
but the output answer is <li>4G LTE</li><li>Dual Sim</li><li>RAM 1GB</li><li>ROM 8GB</li><li>screen 5.5</li><li>Warranty 1 year</li>
Use textContent to get only the innerText???. CC @StevenAndrian
Just use innerText instead of innerHTML
@Tushar yes, i want to get value, for example 4G LTE, Dual Sim, RAM 1GB, ROM 8GB, Screen 5.5, Warranty 1 Year. i want to put comma (,) between every value.
0

You should be able to select every li using querySelectorAll and then map those values. It would look like this:

var listItems = Array.prototype.slice.call(document.querySelectorAll('li'));


var vals = listItems.map(function (item) {
  return item.innerHTML;
});

Example: http://jsbin.com/zumewidoyo/edit?html,js,console

Comments

0

If you want to select every li element you can do something like this:

Live Preview

HTML

<div class="row">
<div style="margin-left:11px">
    <strong>Detail Product</strong>
</div>
    <div class="col-sm-12">
        <ul class="item-highlights">
            <li>4G LTE</li>
            <li>Dual Sim</li>
            <li>RAM 1GB</li>
        </ul>
    </div>
    <div class="col-sm-12">
        <ul class="item-highlights">
            <li>ROM 8GB</li>
            <li>Screen 5.5</li>
            <li>Warranty 1 Year</li>
        </ul>
    </div>

JavaScript

//store the list elements
var lists = document.getElementsByTagName('li');

//array to hold the li elements
var liElements = [];

//loop through the lists
for (var i = 0; i < lists.length; i++) {

  //add the li element values to the array
  liElements.push(lists[i].innerHTML);

}

//show the results
alert(liElements.join("\n"));

Comments

0

The function getElementsByClassName return an array. Just iterate over it instead of using the "[0]" to get only the first element.

function getValue() {
  var test = document.getElementById('block-system-main').getElementsByClassName('item-highlights');
  var array = [];
  for (var i = 0; i < test.length; i++) {
    var liList = test[i].getElementsByTagName('li');
    for (var j = 0; j < liList.length; j++)
      array.push(liList[j].innerHTML);
  }
  return array;
}
alert(getValue());
<div id="block-system-main">
  <div class="row">
    <div style="margin-left:11px">
      <strong>Detail Product</strong>
    </div>
    <div class="col-sm-12">
      <ul class="item-highlights">
        <li>4G LTE</li>
        <li>Dual Sim</li>
        <li>RAM 1GB</li>
      </ul>
    </div>
    <div class="col-sm-12">
      <ul class="item-highlights">
        <li>ROM 8GB</li>
        <li>Screen 5.5</li>
        <li>Warranty 1 Year</li>
      </ul>
    </div>
  </div>
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.