On/Off Button to Activate function










0















I'm looking to have the mouse hover function active only when my button is On and when my button is off have it not activate the hover function. I can get the hover to work but not when its on






function changeBoxColor() 
let myBox = document.getElementById("myBox");
if (myBox.style.backgroundColor === "green")
myBox.style.backgroundColor = "red";
else
myBox.style.backgroundColor = "green";


document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

function changeToggleButton()
let toggleButton = document.getElementById("toggleButton");
if (toggleButton.value === "ON")
toggleButton.value = "OFF";
else
toggleButton.value = "ON";


document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

<input id="toggleButton" type="button" value="ON">
<div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>












share|improve this question



















  • 1





    You have code to change the value of the button, and code to handle the mouseover, but you never seem to combine them and check the button's value when mousing over the div.

    – j08691
    Nov 14 '18 at 19:12















0















I'm looking to have the mouse hover function active only when my button is On and when my button is off have it not activate the hover function. I can get the hover to work but not when its on






function changeBoxColor() 
let myBox = document.getElementById("myBox");
if (myBox.style.backgroundColor === "green")
myBox.style.backgroundColor = "red";
else
myBox.style.backgroundColor = "green";


document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

function changeToggleButton()
let toggleButton = document.getElementById("toggleButton");
if (toggleButton.value === "ON")
toggleButton.value = "OFF";
else
toggleButton.value = "ON";


document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

<input id="toggleButton" type="button" value="ON">
<div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>












share|improve this question



















  • 1





    You have code to change the value of the button, and code to handle the mouseover, but you never seem to combine them and check the button's value when mousing over the div.

    – j08691
    Nov 14 '18 at 19:12













0












0








0








I'm looking to have the mouse hover function active only when my button is On and when my button is off have it not activate the hover function. I can get the hover to work but not when its on






function changeBoxColor() 
let myBox = document.getElementById("myBox");
if (myBox.style.backgroundColor === "green")
myBox.style.backgroundColor = "red";
else
myBox.style.backgroundColor = "green";


document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

function changeToggleButton()
let toggleButton = document.getElementById("toggleButton");
if (toggleButton.value === "ON")
toggleButton.value = "OFF";
else
toggleButton.value = "ON";


document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

<input id="toggleButton" type="button" value="ON">
<div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>












share|improve this question
















I'm looking to have the mouse hover function active only when my button is On and when my button is off have it not activate the hover function. I can get the hover to work but not when its on






function changeBoxColor() 
let myBox = document.getElementById("myBox");
if (myBox.style.backgroundColor === "green")
myBox.style.backgroundColor = "red";
else
myBox.style.backgroundColor = "green";


document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

function changeToggleButton()
let toggleButton = document.getElementById("toggleButton");
if (toggleButton.value === "ON")
toggleButton.value = "OFF";
else
toggleButton.value = "ON";


document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

<input id="toggleButton" type="button" value="ON">
<div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>








function changeBoxColor() 
let myBox = document.getElementById("myBox");
if (myBox.style.backgroundColor === "green")
myBox.style.backgroundColor = "red";
else
myBox.style.backgroundColor = "green";


document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

function changeToggleButton()
let toggleButton = document.getElementById("toggleButton");
if (toggleButton.value === "ON")
toggleButton.value = "OFF";
else
toggleButton.value = "ON";


document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

<input id="toggleButton" type="button" value="ON">
<div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>





function changeBoxColor() 
let myBox = document.getElementById("myBox");
if (myBox.style.backgroundColor === "green")
myBox.style.backgroundColor = "red";
else
myBox.style.backgroundColor = "green";


document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

function changeToggleButton()
let toggleButton = document.getElementById("toggleButton");
if (toggleButton.value === "ON")
toggleButton.value = "OFF";
else
toggleButton.value = "ON";


document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

<input id="toggleButton" type="button" value="ON">
<div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:10









j08691

167k20197215




167k20197215










asked Nov 14 '18 at 19:07









Jayson GJayson G

133




133







  • 1





    You have code to change the value of the button, and code to handle the mouseover, but you never seem to combine them and check the button's value when mousing over the div.

    – j08691
    Nov 14 '18 at 19:12












  • 1





    You have code to change the value of the button, and code to handle the mouseover, but you never seem to combine them and check the button's value when mousing over the div.

    – j08691
    Nov 14 '18 at 19:12







1




1





You have code to change the value of the button, and code to handle the mouseover, but you never seem to combine them and check the button's value when mousing over the div.

– j08691
Nov 14 '18 at 19:12





You have code to change the value of the button, and code to handle the mouseover, but you never seem to combine them and check the button's value when mousing over the div.

– j08691
Nov 14 '18 at 19:12












3 Answers
3






active

oldest

votes


















1














Like j08691 commented above, you just aren't binding the change in EventListeners on load, or change of the toggle button. Here is updated code that does exactly this:



function changeToggleButton() 
let toggleButton = document.getElementById("toggleButton");
if (toggleButton.value === "ON")
toggleButton.value = "OFF";
document.getElementById("myBox").removeEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").removeEventListener("mouseleave", changeBoxColor);
else
toggleButton.value = "ON";
document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);


document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);
document.getElementById("toggleButton").addEventListener("click", changeToggleButton);


Now, this code assumes that the toggleButton starts as on, which is why we automatically addEventListener when the script is loaded. The other change is that when you check the toggleButton.value, we add/remove the EventListener from the element.






share|improve this answer






























    0














    Simple add the events when the button value is 'ON', otherwise remove the events






    function changeBoxColor() 
    let myBox = document.getElementById("myBox");
    if (myBox.style.backgroundColor === "green")
    myBox.style.backgroundColor = "red";
    else
    myBox.style.backgroundColor = "green";



    document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
    document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

    var eventOver = ["mouseover",changeBoxColor];
    var eventLeave = ["mouseleave",changeBoxColor];


    function changeToggleButton()
    let toggleButton = document.getElementById("toggleButton");
    if (toggleButton.value === "ON")
    toggleButton.value = "OFF";
    document.getElementById("myBox").removeEventListener(...eventOver);
    document.getElementById("myBox").removeEventListener(...eventLeave);

    else
    toggleButton.value = "ON";
    document.getElementById("myBox").addEventListener(...eventOver);
    document.getElementById("myBox").addEventListener(...eventLeave);


    document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

    <input id="toggleButton" type="button" value="ON">
    <div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>








    share|improve this answer






























      0














      FWIW, this can be done using HTML and CSS only, if you accept that your 'toggle button' can be a checkbox (a checkbox basically is a toggle button).



      You can then use an attribute selector to find the checkbox, or simply select it using its class or id. Then, using + div and + div:hover, you can style the div after it.



      The trick is in this selector:



      input[type=checkbox]:checked + div:hover 


      Which basically says, target a hovered div, that is right after a checked input of type checkbox.






      input[type=checkbox] + div 
      height: 400px;
      width: 400px;
      background-color: red;


      input[type=checkbox]:checked + div:hover
      background-color:green;

      <input id="toggleButton" type="checkbox" value="">
      <div id="myBox"></div>





      Of course, you can style the checkbox to look more like the button you want, or hide it completely and use a <label for="toggleButton"></label>, which can take the place of the checkbox visually, and be styled however you like.



      Or, you can even use a normal button, and just change the class of the button on click. You can then still use CSS to style the div.



      This could be done using <input, but you'd have to set the value through JavaScript. For the sake of example, I used <button, which has content rather than a value, and so you can toggle the caption as well using CSS, if you would like that.






      (function(element) 
      element.addEventListener('click', function()
      if (element.classList.contains('on'))
      element.classList.remove('on');
      else
      element.classList.add('on');
      );
      )(document.getElementById('toggleButton'));

      button + div 
      height: 400px;
      width: 400px;
      background-color: red;


      button.on + div:hover
      background-color:green;


      /* If you like, you can even set the button text in CSS, but
      beware of accessibility issues. */
      button:after
      content: "off";

      button.on:after
      content: "on";

      <button id="toggleButton" type="button"></button>
      <div id="myBox"></div>








      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307170%2fon-off-button-to-activate-function%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Like j08691 commented above, you just aren't binding the change in EventListeners on load, or change of the toggle button. Here is updated code that does exactly this:



        function changeToggleButton() 
        let toggleButton = document.getElementById("toggleButton");
        if (toggleButton.value === "ON")
        toggleButton.value = "OFF";
        document.getElementById("myBox").removeEventListener("mouseover", changeBoxColor);
        document.getElementById("myBox").removeEventListener("mouseleave", changeBoxColor);
        else
        toggleButton.value = "ON";
        document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
        document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);


        document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
        document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);
        document.getElementById("toggleButton").addEventListener("click", changeToggleButton);


        Now, this code assumes that the toggleButton starts as on, which is why we automatically addEventListener when the script is loaded. The other change is that when you check the toggleButton.value, we add/remove the EventListener from the element.






        share|improve this answer



























          1














          Like j08691 commented above, you just aren't binding the change in EventListeners on load, or change of the toggle button. Here is updated code that does exactly this:



          function changeToggleButton() 
          let toggleButton = document.getElementById("toggleButton");
          if (toggleButton.value === "ON")
          toggleButton.value = "OFF";
          document.getElementById("myBox").removeEventListener("mouseover", changeBoxColor);
          document.getElementById("myBox").removeEventListener("mouseleave", changeBoxColor);
          else
          toggleButton.value = "ON";
          document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
          document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);


          document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
          document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);
          document.getElementById("toggleButton").addEventListener("click", changeToggleButton);


          Now, this code assumes that the toggleButton starts as on, which is why we automatically addEventListener when the script is loaded. The other change is that when you check the toggleButton.value, we add/remove the EventListener from the element.






          share|improve this answer

























            1












            1








            1







            Like j08691 commented above, you just aren't binding the change in EventListeners on load, or change of the toggle button. Here is updated code that does exactly this:



            function changeToggleButton() 
            let toggleButton = document.getElementById("toggleButton");
            if (toggleButton.value === "ON")
            toggleButton.value = "OFF";
            document.getElementById("myBox").removeEventListener("mouseover", changeBoxColor);
            document.getElementById("myBox").removeEventListener("mouseleave", changeBoxColor);
            else
            toggleButton.value = "ON";
            document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
            document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);


            document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
            document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);
            document.getElementById("toggleButton").addEventListener("click", changeToggleButton);


            Now, this code assumes that the toggleButton starts as on, which is why we automatically addEventListener when the script is loaded. The other change is that when you check the toggleButton.value, we add/remove the EventListener from the element.






            share|improve this answer













            Like j08691 commented above, you just aren't binding the change in EventListeners on load, or change of the toggle button. Here is updated code that does exactly this:



            function changeToggleButton() 
            let toggleButton = document.getElementById("toggleButton");
            if (toggleButton.value === "ON")
            toggleButton.value = "OFF";
            document.getElementById("myBox").removeEventListener("mouseover", changeBoxColor);
            document.getElementById("myBox").removeEventListener("mouseleave", changeBoxColor);
            else
            toggleButton.value = "ON";
            document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
            document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);


            document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
            document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);
            document.getElementById("toggleButton").addEventListener("click", changeToggleButton);


            Now, this code assumes that the toggleButton starts as on, which is why we automatically addEventListener when the script is loaded. The other change is that when you check the toggleButton.value, we add/remove the EventListener from the element.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 19:18









            RyanRyan

            31619




            31619























                0














                Simple add the events when the button value is 'ON', otherwise remove the events






                function changeBoxColor() 
                let myBox = document.getElementById("myBox");
                if (myBox.style.backgroundColor === "green")
                myBox.style.backgroundColor = "red";
                else
                myBox.style.backgroundColor = "green";



                document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
                document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

                var eventOver = ["mouseover",changeBoxColor];
                var eventLeave = ["mouseleave",changeBoxColor];


                function changeToggleButton()
                let toggleButton = document.getElementById("toggleButton");
                if (toggleButton.value === "ON")
                toggleButton.value = "OFF";
                document.getElementById("myBox").removeEventListener(...eventOver);
                document.getElementById("myBox").removeEventListener(...eventLeave);

                else
                toggleButton.value = "ON";
                document.getElementById("myBox").addEventListener(...eventOver);
                document.getElementById("myBox").addEventListener(...eventLeave);


                document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

                <input id="toggleButton" type="button" value="ON">
                <div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>








                share|improve this answer



























                  0














                  Simple add the events when the button value is 'ON', otherwise remove the events






                  function changeBoxColor() 
                  let myBox = document.getElementById("myBox");
                  if (myBox.style.backgroundColor === "green")
                  myBox.style.backgroundColor = "red";
                  else
                  myBox.style.backgroundColor = "green";



                  document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
                  document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

                  var eventOver = ["mouseover",changeBoxColor];
                  var eventLeave = ["mouseleave",changeBoxColor];


                  function changeToggleButton()
                  let toggleButton = document.getElementById("toggleButton");
                  if (toggleButton.value === "ON")
                  toggleButton.value = "OFF";
                  document.getElementById("myBox").removeEventListener(...eventOver);
                  document.getElementById("myBox").removeEventListener(...eventLeave);

                  else
                  toggleButton.value = "ON";
                  document.getElementById("myBox").addEventListener(...eventOver);
                  document.getElementById("myBox").addEventListener(...eventLeave);


                  document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

                  <input id="toggleButton" type="button" value="ON">
                  <div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>








                  share|improve this answer

























                    0












                    0








                    0







                    Simple add the events when the button value is 'ON', otherwise remove the events






                    function changeBoxColor() 
                    let myBox = document.getElementById("myBox");
                    if (myBox.style.backgroundColor === "green")
                    myBox.style.backgroundColor = "red";
                    else
                    myBox.style.backgroundColor = "green";



                    document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
                    document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

                    var eventOver = ["mouseover",changeBoxColor];
                    var eventLeave = ["mouseleave",changeBoxColor];


                    function changeToggleButton()
                    let toggleButton = document.getElementById("toggleButton");
                    if (toggleButton.value === "ON")
                    toggleButton.value = "OFF";
                    document.getElementById("myBox").removeEventListener(...eventOver);
                    document.getElementById("myBox").removeEventListener(...eventLeave);

                    else
                    toggleButton.value = "ON";
                    document.getElementById("myBox").addEventListener(...eventOver);
                    document.getElementById("myBox").addEventListener(...eventLeave);


                    document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

                    <input id="toggleButton" type="button" value="ON">
                    <div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>








                    share|improve this answer













                    Simple add the events when the button value is 'ON', otherwise remove the events






                    function changeBoxColor() 
                    let myBox = document.getElementById("myBox");
                    if (myBox.style.backgroundColor === "green")
                    myBox.style.backgroundColor = "red";
                    else
                    myBox.style.backgroundColor = "green";



                    document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
                    document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

                    var eventOver = ["mouseover",changeBoxColor];
                    var eventLeave = ["mouseleave",changeBoxColor];


                    function changeToggleButton()
                    let toggleButton = document.getElementById("toggleButton");
                    if (toggleButton.value === "ON")
                    toggleButton.value = "OFF";
                    document.getElementById("myBox").removeEventListener(...eventOver);
                    document.getElementById("myBox").removeEventListener(...eventLeave);

                    else
                    toggleButton.value = "ON";
                    document.getElementById("myBox").addEventListener(...eventOver);
                    document.getElementById("myBox").addEventListener(...eventLeave);


                    document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

                    <input id="toggleButton" type="button" value="ON">
                    <div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>








                    function changeBoxColor() 
                    let myBox = document.getElementById("myBox");
                    if (myBox.style.backgroundColor === "green")
                    myBox.style.backgroundColor = "red";
                    else
                    myBox.style.backgroundColor = "green";



                    document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
                    document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

                    var eventOver = ["mouseover",changeBoxColor];
                    var eventLeave = ["mouseleave",changeBoxColor];


                    function changeToggleButton()
                    let toggleButton = document.getElementById("toggleButton");
                    if (toggleButton.value === "ON")
                    toggleButton.value = "OFF";
                    document.getElementById("myBox").removeEventListener(...eventOver);
                    document.getElementById("myBox").removeEventListener(...eventLeave);

                    else
                    toggleButton.value = "ON";
                    document.getElementById("myBox").addEventListener(...eventOver);
                    document.getElementById("myBox").addEventListener(...eventLeave);


                    document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

                    <input id="toggleButton" type="button" value="ON">
                    <div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>





                    function changeBoxColor() 
                    let myBox = document.getElementById("myBox");
                    if (myBox.style.backgroundColor === "green")
                    myBox.style.backgroundColor = "red";
                    else
                    myBox.style.backgroundColor = "green";



                    document.getElementById("myBox").addEventListener("mouseover", changeBoxColor);
                    document.getElementById("myBox").addEventListener("mouseleave", changeBoxColor);

                    var eventOver = ["mouseover",changeBoxColor];
                    var eventLeave = ["mouseleave",changeBoxColor];


                    function changeToggleButton()
                    let toggleButton = document.getElementById("toggleButton");
                    if (toggleButton.value === "ON")
                    toggleButton.value = "OFF";
                    document.getElementById("myBox").removeEventListener(...eventOver);
                    document.getElementById("myBox").removeEventListener(...eventLeave);

                    else
                    toggleButton.value = "ON";
                    document.getElementById("myBox").addEventListener(...eventOver);
                    document.getElementById("myBox").addEventListener(...eventLeave);


                    document.getElementById("toggleButton").addEventListener("click", changeToggleButton);

                    <input id="toggleButton" type="button" value="ON">
                    <div style="height: 400px; width: 400px; background-color: red;" id="myBox"></div>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 19:17









                    Gabriel LopezGabriel Lopez

                    28217




                    28217





















                        0














                        FWIW, this can be done using HTML and CSS only, if you accept that your 'toggle button' can be a checkbox (a checkbox basically is a toggle button).



                        You can then use an attribute selector to find the checkbox, or simply select it using its class or id. Then, using + div and + div:hover, you can style the div after it.



                        The trick is in this selector:



                        input[type=checkbox]:checked + div:hover 


                        Which basically says, target a hovered div, that is right after a checked input of type checkbox.






                        input[type=checkbox] + div 
                        height: 400px;
                        width: 400px;
                        background-color: red;


                        input[type=checkbox]:checked + div:hover
                        background-color:green;

                        <input id="toggleButton" type="checkbox" value="">
                        <div id="myBox"></div>





                        Of course, you can style the checkbox to look more like the button you want, or hide it completely and use a <label for="toggleButton"></label>, which can take the place of the checkbox visually, and be styled however you like.



                        Or, you can even use a normal button, and just change the class of the button on click. You can then still use CSS to style the div.



                        This could be done using <input, but you'd have to set the value through JavaScript. For the sake of example, I used <button, which has content rather than a value, and so you can toggle the caption as well using CSS, if you would like that.






                        (function(element) 
                        element.addEventListener('click', function()
                        if (element.classList.contains('on'))
                        element.classList.remove('on');
                        else
                        element.classList.add('on');
                        );
                        )(document.getElementById('toggleButton'));

                        button + div 
                        height: 400px;
                        width: 400px;
                        background-color: red;


                        button.on + div:hover
                        background-color:green;


                        /* If you like, you can even set the button text in CSS, but
                        beware of accessibility issues. */
                        button:after
                        content: "off";

                        button.on:after
                        content: "on";

                        <button id="toggleButton" type="button"></button>
                        <div id="myBox"></div>








                        share|improve this answer





























                          0














                          FWIW, this can be done using HTML and CSS only, if you accept that your 'toggle button' can be a checkbox (a checkbox basically is a toggle button).



                          You can then use an attribute selector to find the checkbox, or simply select it using its class or id. Then, using + div and + div:hover, you can style the div after it.



                          The trick is in this selector:



                          input[type=checkbox]:checked + div:hover 


                          Which basically says, target a hovered div, that is right after a checked input of type checkbox.






                          input[type=checkbox] + div 
                          height: 400px;
                          width: 400px;
                          background-color: red;


                          input[type=checkbox]:checked + div:hover
                          background-color:green;

                          <input id="toggleButton" type="checkbox" value="">
                          <div id="myBox"></div>





                          Of course, you can style the checkbox to look more like the button you want, or hide it completely and use a <label for="toggleButton"></label>, which can take the place of the checkbox visually, and be styled however you like.



                          Or, you can even use a normal button, and just change the class of the button on click. You can then still use CSS to style the div.



                          This could be done using <input, but you'd have to set the value through JavaScript. For the sake of example, I used <button, which has content rather than a value, and so you can toggle the caption as well using CSS, if you would like that.






                          (function(element) 
                          element.addEventListener('click', function()
                          if (element.classList.contains('on'))
                          element.classList.remove('on');
                          else
                          element.classList.add('on');
                          );
                          )(document.getElementById('toggleButton'));

                          button + div 
                          height: 400px;
                          width: 400px;
                          background-color: red;


                          button.on + div:hover
                          background-color:green;


                          /* If you like, you can even set the button text in CSS, but
                          beware of accessibility issues. */
                          button:after
                          content: "off";

                          button.on:after
                          content: "on";

                          <button id="toggleButton" type="button"></button>
                          <div id="myBox"></div>








                          share|improve this answer



























                            0












                            0








                            0







                            FWIW, this can be done using HTML and CSS only, if you accept that your 'toggle button' can be a checkbox (a checkbox basically is a toggle button).



                            You can then use an attribute selector to find the checkbox, or simply select it using its class or id. Then, using + div and + div:hover, you can style the div after it.



                            The trick is in this selector:



                            input[type=checkbox]:checked + div:hover 


                            Which basically says, target a hovered div, that is right after a checked input of type checkbox.






                            input[type=checkbox] + div 
                            height: 400px;
                            width: 400px;
                            background-color: red;


                            input[type=checkbox]:checked + div:hover
                            background-color:green;

                            <input id="toggleButton" type="checkbox" value="">
                            <div id="myBox"></div>





                            Of course, you can style the checkbox to look more like the button you want, or hide it completely and use a <label for="toggleButton"></label>, which can take the place of the checkbox visually, and be styled however you like.



                            Or, you can even use a normal button, and just change the class of the button on click. You can then still use CSS to style the div.



                            This could be done using <input, but you'd have to set the value through JavaScript. For the sake of example, I used <button, which has content rather than a value, and so you can toggle the caption as well using CSS, if you would like that.






                            (function(element) 
                            element.addEventListener('click', function()
                            if (element.classList.contains('on'))
                            element.classList.remove('on');
                            else
                            element.classList.add('on');
                            );
                            )(document.getElementById('toggleButton'));

                            button + div 
                            height: 400px;
                            width: 400px;
                            background-color: red;


                            button.on + div:hover
                            background-color:green;


                            /* If you like, you can even set the button text in CSS, but
                            beware of accessibility issues. */
                            button:after
                            content: "off";

                            button.on:after
                            content: "on";

                            <button id="toggleButton" type="button"></button>
                            <div id="myBox"></div>








                            share|improve this answer















                            FWIW, this can be done using HTML and CSS only, if you accept that your 'toggle button' can be a checkbox (a checkbox basically is a toggle button).



                            You can then use an attribute selector to find the checkbox, or simply select it using its class or id. Then, using + div and + div:hover, you can style the div after it.



                            The trick is in this selector:



                            input[type=checkbox]:checked + div:hover 


                            Which basically says, target a hovered div, that is right after a checked input of type checkbox.






                            input[type=checkbox] + div 
                            height: 400px;
                            width: 400px;
                            background-color: red;


                            input[type=checkbox]:checked + div:hover
                            background-color:green;

                            <input id="toggleButton" type="checkbox" value="">
                            <div id="myBox"></div>





                            Of course, you can style the checkbox to look more like the button you want, or hide it completely and use a <label for="toggleButton"></label>, which can take the place of the checkbox visually, and be styled however you like.



                            Or, you can even use a normal button, and just change the class of the button on click. You can then still use CSS to style the div.



                            This could be done using <input, but you'd have to set the value through JavaScript. For the sake of example, I used <button, which has content rather than a value, and so you can toggle the caption as well using CSS, if you would like that.






                            (function(element) 
                            element.addEventListener('click', function()
                            if (element.classList.contains('on'))
                            element.classList.remove('on');
                            else
                            element.classList.add('on');
                            );
                            )(document.getElementById('toggleButton'));

                            button + div 
                            height: 400px;
                            width: 400px;
                            background-color: red;


                            button.on + div:hover
                            background-color:green;


                            /* If you like, you can even set the button text in CSS, but
                            beware of accessibility issues. */
                            button:after
                            content: "off";

                            button.on:after
                            content: "on";

                            <button id="toggleButton" type="button"></button>
                            <div id="myBox"></div>








                            input[type=checkbox] + div 
                            height: 400px;
                            width: 400px;
                            background-color: red;


                            input[type=checkbox]:checked + div:hover
                            background-color:green;

                            <input id="toggleButton" type="checkbox" value="">
                            <div id="myBox"></div>





                            input[type=checkbox] + div 
                            height: 400px;
                            width: 400px;
                            background-color: red;


                            input[type=checkbox]:checked + div:hover
                            background-color:green;

                            <input id="toggleButton" type="checkbox" value="">
                            <div id="myBox"></div>





                            (function(element) 
                            element.addEventListener('click', function()
                            if (element.classList.contains('on'))
                            element.classList.remove('on');
                            else
                            element.classList.add('on');
                            );
                            )(document.getElementById('toggleButton'));

                            button + div 
                            height: 400px;
                            width: 400px;
                            background-color: red;


                            button.on + div:hover
                            background-color:green;


                            /* If you like, you can even set the button text in CSS, but
                            beware of accessibility issues. */
                            button:after
                            content: "off";

                            button.on:after
                            content: "on";

                            <button id="toggleButton" type="button"></button>
                            <div id="myBox"></div>





                            (function(element) 
                            element.addEventListener('click', function()
                            if (element.classList.contains('on'))
                            element.classList.remove('on');
                            else
                            element.classList.add('on');
                            );
                            )(document.getElementById('toggleButton'));

                            button + div 
                            height: 400px;
                            width: 400px;
                            background-color: red;


                            button.on + div:hover
                            background-color:green;


                            /* If you like, you can even set the button text in CSS, but
                            beware of accessibility issues. */
                            button:after
                            content: "off";

                            button.on:after
                            content: "on";

                            <button id="toggleButton" type="button"></button>
                            <div id="myBox"></div>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 14 '18 at 19:49

























                            answered Nov 14 '18 at 19:32









                            GolezTrolGolezTrol

                            99.4k10134176




                            99.4k10134176



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307170%2fon-off-button-to-activate-function%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Darth Vader #20

                                How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                Ondo