I'm testing a js function to check and uncheck a checkbox with a separate div which also changes color.
The following script works when the checkbox id is typed into the function manually, but when I try to pass it in as a parameter it doesn't work. Am I doing something wrong?
<form action="#">
<input type="checkbox" id="joe" name="joe">
<div id="container" style="background-color: red; width: 200px; height: 200px;" onclick="selected(this, 'joe')"></div>
<script>
function selected(elmnt, id2check) {
var x = document.getElementById("id2check").checked;
if(x == false) {
elmnt.style.backgroundColor = "blue";
document.getElementById("id2check").checked = true;
} else {
elmnt.style.backgroundColor = "red";
document.getElementById("id2check").checked = false;
}
}
</script>
UPDATE:
Ah, just figured it out. The tutorial I was basing it on used quotation marks on the passed value. I took those off and it works now. :)
Aucun commentaire:
Enregistrer un commentaire