Reputation: 31
i am having 10 check boxes.if i click the value of the check box has to push to the array,if i unchecked the check box then the corresponding check box value has to be deleted from the array? pls...anyone give reply for this
Upvotes: 3
Views: 131
Reputation: 607
function getChecked(ename)
{
field = document.getElementsByName(ename);
var topub = new Array();
for (i = 0; i < field.length; i++)
if(field[i].checked == true)
{
topub.push(field[i].id);
}
return topub;
}
Above is what i used in such cases. i keep name of all checkboxes same and then pass name to this function and it returns array. So you can replace ur array with returned one. Like :
var yourOldArray = new Array();<br>
yourOldArray = getChecked("name attribute of checkboxes");
All you need to make sure is all checkboxes have same value for their name attribute.
Upvotes: 0
Reputation: 17683
try javascript associative array like:
a['title'] = 'Mr.';
a["fname'] = 'Brad';
a['lname'] = 'Johnson';
delete a['title'];
Its simple... when user checks it adds with its id as key and when unchecks it deletes
Upvotes: 0
Reputation: 38431
Does this have to work exactly like this? It would be easier to generate an array of values from the checked checkboxes when needed.
Something like:
// Pass reference to a parent element of all checkboxes, for example the form
function getCheckedBoxes(parent) {
var result = [];
var inputs = parent.getElementsByTagName("input");
for (var i = 0, len = inputs.length; i < len; i++) {
var cb = inputs[i];
if (cb.type.toLowerCase() === "checkbox" && cb.checked)
result.push(cb.value);
}
return result;
}
Upvotes: 1
Reputation: 15571
Use array.splice(index,howmany,element1,.....,elementX)
. See more about splice here: http://www.w3schools.com/jsref/jsref_splice.asp .
Upvotes: 0