JavaScript Get checkbox status using javascript checkbox status in javascript,checkbox value in javascript,checkbox state

This is my checkbox HTML code

<input id="termsCheckbox" name="termsCheckbox" type="checkbox" value="terms" <?PHP echo $terms; ?> class="checkbox">

this is javascript code

var terms = $("#termsCheckbox");

function validateTerms(){
if(termsCheckbox.checked == false){
terms_div.addClass("terms_error");
return false;
}
else{           
terms_div.removeClass("terms_error");
return true;
}
}

I want to check whether checkbox checked or not and if not add a class to terms_div. Please help me to solve this problem. thanks

Answer:1

You need to access the className variable (pure JS) the following assumes your div has an ID of terms_div, that terms_error is the only class you might want on the div, and that you setup your checkbox with onClick="validateTerms();"

function validateTerms(){
  var c=document.getElementById('termsCheckbox');
  var d=document.getElementById('terms_div');
  if (c.checked) {
    d.className='';
    return true;
  } else { 
    d.className='terms_error';
    return false;
  }
}
Answer:2

Simply bind an onchange handler to your checkbox.

$("#termsCheckbox").change(function() {

    // class will be removed if checked="checked"
    // otherwise will be added
    $(this).toggleClass("terms_error", !this.checked);
}).change(); // set initial state
Answer:3

Live Demo (Click the "Terms Div" text to test)

I didnt see the question tagged with jQuery, but I noticed a jQery selector was used.. so just to be safe I did it with pure JS anyway.

Pure JS

var terms = document.getElementById("termsCheckbox"),
    terms_div = document.getElementById("terms_div");

function validateTerms(){
    if(terms.checked == false){
        if(terms_div.className.indexOf("terms_error")<0){
            terms_div.className += " terms_error";
        }
        return false;
    }else{      
        terms_div.className = terms_div.className.replace(/\bterms_error\b/,'');
        return true;
    }
}
Answer:4

try adding onclick="validateTerms();" on the checkbox tag

Answer:5

I currently work on the following page: divResizable has been modified by jQuery using the following code in $().ready() $("#divResizable").resizable({ handles: 'e' }); $("#divResizable").bind("...

I currently work on the following page: divResizable has been modified by jQuery using the following code in $().ready() $("#divResizable").resizable({ handles: 'e' }); $("#divResizable").bind("...

  1. set div width using jquery
  2. set div width in jquery
  3. change width of div using jquery
  4. change div width in jquery

im trying to make my own chat... so i have an input text field, the submit button, isn't even submit, its just a button.... so when the enter key is pressed, i need the value of the input field to ...

im trying to make my own chat... so i have an input text field, the submit button, isn't even submit, its just a button.... so when the enter key is pressed, i need the value of the input field to ...

So QUnit provides the "raise" assertion to test if an exception is thrown. Is there any way to test the actual message thrown by the exception, though? For instance, say I have this function: ...

So QUnit provides the "raise" assertion to test if an exception is thrown. Is there any way to test the actual message thrown by the exception, though? For instance, say I have this function: ...

  1. junit test exception message
  2. java junit test exception message
  3. junit test expected exception message
  4. junit 5 test exception message
  5. junit test exception thrown message
  6. junit test check exception message
  7. junit 4 test exception message
  8. junit test assert exception message
  9. junit test catch exception message
  10. junit test custom exception message
  11. junit test annotation expected exception message
  12. junit test verify exception message

I need a mechanism for storing complex data structures created in client side javascript. I've been considering using the stringify method to convert the javascript object into a string, store it in ...

I need a mechanism for storing complex data structures created in client side javascript. I've been considering using the stringify method to convert the javascript object into a string, store it in ...