Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Checkboxes with Javascript Functions

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>
like image 543
HondaKillrsx Avatar asked Dec 09 '25 15:12

HondaKillrsx


1 Answers

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) {
like image 175
Joe Enos Avatar answered Dec 12 '25 05:12

Joe Enos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!