11

i have tried this:

    function stickyheaddsadaer() {
        $("#page-header-inner").addClass("sticky");
    }

    <input type="checkbox" name="TT_sticky_header" id="TT_sticky_header_function" 
value="{TT_sticky_header}" onclick="stickyheaddsadaer()"/>

so when i click checkbox, it just nothing happens....

but when i try this:

function stickyheaddsadaer() {
    alert("I am an alert box!");
}

then works...

can you please help me ? I need to activate javasript function by checkbox

and when the js function is actived it add class to the div

Thank you

2
  • Just check if the element "page-header-inner" exists.Can you please show what your css class sticky has Commented Oct 15, 2016 at 5:43
  • it existes, please see photo: prntscr.com/cu1fdy Commented Oct 15, 2016 at 5:50

3 Answers 3

20

Better to use onchange event and check inside function if checked or not

function stickyheaddsadaer(obj) {
  if($(obj).is(":checked")){
    alert("Yes checked"); //when checked
    $("#page-header-inner").addClass("sticky");
  }else{
    alert("Not checked"); //when not checked
  }
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="TT_sticky_header" id="TT_sticky_header_function" value="{TT_sticky_header}" onchange="stickyheaddsadaer(this)"/>

Sign up to request clarification or add additional context in comments.

2 Comments

but i have another problem now,
5

HTML

<input type="checkbox" id="switch" onclick="change()">

JS

function change() {
    var decider = document.getElementById('switch');
    if(decider.checked){
        alert('check');
    } else {
      alert('unchecked');
    }
}

Comments

1

function checkFluency(){
    var checkbox = document.getElementById('fluency');
    if (checkbox.checked != false) {
	    alert("Checkbox checked")
    }else{
	    alert("Not Checked")
    }
}
<input type="checkbox" onclick="checkFluency()"  id="fluency" />

1 Comment

This would be much more helpful if it included a description of how it solves the question. This also looks like it is the same as the answer by @rootleet 2 months earlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.