Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
98
rated 0 times [  101] [ 3]  / answers: 1 / hits: 24735  / 12 Years ago, sat, october 27, 2012, 12:00:00

I'm just trying to get this checkbox to alert a message after it is checked and after it is unchecked by running a function in Javascript. I can get it to display the checked message but can't get the unchecked alert to come up.



<input type=checkbox id=chbx onchange=foo()>
<script type=text/javascript>
var checkbox = document.getElementById(chbx);

function foo(){
if(checkbox.checked=true){
alert(Checked!);
}
else {
alert(UnChecked!);
}
};
</script>

More From » html

 Answers
32

You've got single-equals instead of double-equals in your if statements:



if (checkbox.checked=true) {


should be



if (checkbox.checked == true) {


or just



if (checkbox.checked) {

[#82320] Thursday, October 25, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pierre

Total Points: 716
Total Questions: 128
Total Answers: 102

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;