javascript - create simple js object and store them into js array -


inside html dom have following structure

<div id="mytitles"> <ul>   <li>       title:<title>a</title><br>       second title: <sectitle>b</sectitle><br>       third title: <thirdtitle>3</thirdtitle>   </li>   <hr>   <li>       title:<title>b</title><br>       second title: <sectitle>c</sectitle><br>       third title: <thirdtitle>9</thirdtitle>   </li>    </ul> </div> 

number of list elements inside ul of mytitles div unknown on runtime. i'm taking var nroftitles = $('#mytitles ul li').length; determine how many there.

now, want create simple javascript object hold values (stored inside semantic tags (title, sectitle, thirdtitle)) , store them inside js array.

var items = $("#mytitles > ul > li");  var arr = [];  items.each(function(){     arr.push([$(this).find("title").text(),$(this).find("sectitle").text(),$(this).find("thirdtitle").text()]) }) 

with current combination of <li>s contain:

[["a","b","3"],["b","c","9"]] 

or can store properties objects:

var items = $("#mytitles > ul > li");  var arr = [];  items.each(function(){     arr.push({title:$(this).find("title").text(),sectitle:$(this).find("sectitle").text(),thirdtitle:$(this).find("thirdtitle").text()}) }) 

then get

[{title:"a",sectitle:"b",thirdtitle:"3"},{title:"b",sectitle:"c",thirdtitle:"9"}] 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -