javascript - getting the value of checked checkbox -
i trying value of checked checkboxes problem dynamic on web page eg :
while ($row=myqli_fetch_array($result)){ echo"<div>"; echo"<select id=\"course\" onchange=getcheckbox()> <option>1</option> <option>2</option> </select>"; echo"<div id=\"checkboxarea\">"; echo"<buttton id=\"w\" >w</button></div>"; }
the ajax call getcheckbok()
result server , on checkboxarea result :
echo "<input type=\"checkbox\" value=\"aaa\" class=\"stdcheckbox\">"; echo "<input type=\"checkbox\" value=\"bbb\" class=\"stdcheckbox\">";
when pressing on button w need value of checked checkbox how can ?
i have tried jquery code :
var x=$(this).parent().find(".stdcheckbox").value;
vanilla javascript solution create array of values of checked <input type="checkbox"/>
s
function checked_values(node) { var checked; if (!node) node = document; checked = node.queryselectorall('input[type="checkbox"]:checked'); return array.prototype.map.call(checked, function (e) {return e.value;}); }
function checked_values(node) { var checked; if (!node) node = document; checked = node.queryselectorall('input[type="checkbox"]:checked'); return array.prototype.map.call(checked, function (e) {return e.value;}); } window.addeventlistener('load', function () { document.queryselector('button').addeventlistener('click', function () { console.log(checked_values()); }); });
<label><input type="checkbox" value="a"/>a</label><br/> <label><input type="checkbox" value="b"/>b</label><br/> <label><input type="checkbox" value="c"/>c</label><br/> <label><input type="checkbox" value="d"/>d</label><br/> <button>test (console)</button>
Comments
Post a Comment