Javascript removing an array of elements

I have 10 flags. If I click, the value of the flag should click on the array, if I unchecked, then the corresponding value of the flag should be removed from the array? Pls ... any answer to that

+3
source share
4 answers

Should this work that way? It would be easier to create an array of values ​​from the checked checkboxes, if necessary.

Sort of:

// 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;
}
+1
source

Use array.splice(index,howmany,element1,.....,elementX). More about splicing here: http://www.w3schools.com/jsref/jsref_splice.asp .

0
source

javascript, :

a['title'] = 'Mr.'; 
a["fname'] = 'Brad'; 
a['lname'] = 'Johnson'; 
delete a['title'];

... , , ,

0
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 the name of all the flags the same, and then pass the name of this function and return an array. That way you can replace ur array with returned one. How:

var yourOldArray = new Array();<br>
yourOldArray = getChecked("name attribute of checkboxes");

All you need to make sure that all the checkboxes have the same value for the name attribute.

0
source

Source: https://habr.com/ru/post/1748815/


All Articles