javascript - Get the value of an element whose id changes -
i trying values of multiple inputs on page. ids of inputs generated dynamically.
<input id="updates_662224305" class="text quantity" type="text" value="1" name="updates[662224305]" size="4">
i running loop table , cells in inputs contained. getting innerhtml of cells inputs located in, , slicing section ids are:
var racetamarray = [], racetamtotal = 0, table = document.getelementbyid('cart-table'), cells = table.getelementsbytagname('td'); (var i=0,len=cells.length; i<len; i++){ if(cells[i].innerhtml.indexof("ncombo-racetam") != -1) { i+=3; var racetaminput = cells[i].innerhtml; var racetaminputcontain = racetaminput.slice(43,60); var racetamelem = document.getelementbyid(racetaminputid); racetamarray.push(parseint(racetamelem.value)); } }
this worked in firefox, able extract ids , put them in variable racetamelem
. when tried in chrome not work, slice occurs @ different section of string , not capture ids. there better way slice or of converting inputs string dom element?
html of 1 of rows in table:
<tr class="item sampler-pramiracetam-capsules"> <td> <span class="ncombo-racetam"></span> <a href="/products/sampler-pramiracetam-capsules"> <img src="//cdn.shopify.com/s/files/1/0173/1766/products/pramiracetamcaps_grande_thumb.jpg?v=1396441752" alt="sampler, pramiracetam capsules" /> </a> </td> <td> <a href="/products/sampler-pramiracetam-capsules">sampler, pramiracetam capsules - 30 capsules</a></td> <td>$8.90</td> <td><input class="text quantity" type="text" size="4" id="updates_658967781" name="updates[658967781]" value="1" class="replace" /></td> <td>$8.90</td> <td><a class="btn remove-from-cart" href="/cart/change?id=658967781&quantity=0">remove</a></td> </tr>
this walks through table rows , checks elements in first cell class name, , if found push input value (from fourth cell) array.
demo: http://jsfiddle.net/seae4/
var racetamarray = []; var table = document.getelementbyid('cart-table'); var row; for(var i=0; i<table.rows.length; i++){ row = table.rows[i]; if(row.cells.length > 3){ if(row.cells[0].queryselectorall('.ncombo-racetam').length) { racetamarray.push(row.cells[3].getelementsbytagname('input')[0].value); } } } console.log(racetamarray);
side note: if need support ie7 or older need replace queryselectorall()
call more code check each element class.
Comments
Post a Comment