Eddy
Eddy

Reputation: 21

how to set checkbox checked and do it onclick together?

My code is only let checkbox checked,I want it also do onclick event.

<input type="checkbox" name="list" id="mycheckbox"  onclick="check_marker(this)" />
document.getElementById(mycheckbox).checked = true;//I want also do onclick event.
    function check_marker(input_var){
    var carId;
    carId = input_var.id;
    alert(carId);
}

Upvotes: 0

Views: 917

Answers (1)

James Montagne
James Montagne

Reputation: 78650

Changes made through javascript do not trigger handlers. You will need to call it yourself after making the change:

http://jsfiddle.net/F2J37/

var cb = document.getElementById("mycheckbox");

cb.checked = true;
cb.onclick();  // call the click handler directly

Upvotes: 1

Related Questions