javascript xpath - get element value and its position, and save results in object array -


i have markup resembling following (note: spans optional child elements):

<li class="prices">    11.11    <span>1.11</span> </li> <li class="prices">     22.22 </li> <li class="prices">    33.33    <span>3.33</span> </li> <li class="prices">    44.44 </li> <li class="prices">    55.55    <span>5.55</span> </li> 

i want return list of prices within spans , position in object array, like: { pos: 0, price, 1.11 }, { pos: 2, price, 3.33 }, { pos: 4, price, 5.55 } what's js code that? :)

assuming have container element contains li.prices elements:

var container = document.getelementbyid("container"); var arr = []; (var pos = 0; pos < container.children.length; pos++) {     var li = container.children[pos];     var spans = li.getelementsbytagname("span");     var price = spans.length > 0 ? spans[0].innerhtml : "";     arr.push({pos: pos, price: price}); } 

this store price string , use empty string if there no span element inside li element.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -