Currently an event is set on checkboxes
, and event.target
gives me the status (checked = true/false)
of checkbox which is clicked.
I am maintaining an object which keeps the track on all the selected checkboxes
var selectedMap = {};
if(event.target == true){
var key = event.target.id;
var val = event.target.name;
selectedMap[key] = val;
}
and I want to remove the element from the map which is unselected
else if(event.target == false){
selectedMap.remove(event.target.id);
}
when I run this it gives me error in Firebug
: selectedMap.remove is not a function
So my question is How can I remove the element when the checkbox is unselected ?
Best Answer
Using
delete
:You're setting the value incorrectly, though. Here's the correct way:
In fact, you could:
Getting the event target stuff out of the way, it's easier to envision this with simple strings:
When using a plain object like this, I always use a prefix on my keys to avoid issues. (For instance, what would happen if your target element's ID was "toString"? The object already has an [inherited] property called "toString" and things would get Very Weird Very Quickly.)
So for me, I do this:
...and of course: