0

I am using Bootstrap Switch to turn my check-box in toggle switches.

Now I want to display different messages based on whether the switch is ON or OFF. Sounds simple enough...but I can't seem to make it work :(

My HTML:

<div class="approve-delivery">
  <label for="config-email" class="control-label col-xs-5">
    Do you want to send out email?
  </label>
  <input type="checkbox" id="config-email" onclick="ToggleSwitchMessage('config-email', 'enable-msg', 'disable-msg')" checked/>
</div>

<div class="email-config-msg">
  <p id="enable-msg">
      All emails will be sent.
  </p>
  <p id="disable-msg">
      No email will be sent.<br/>
      Please remember, you must turn email ON again if you want emails to
      be sent during this login session.
  </p>
</div>

And my jQuery:

function ToggleSwitchMessage(checkId, firstCommentId, secondCommentId) {
  var check_box = document.getElementById(checkId)
  var first_alert = document.getElementById(firstCommentId);
  var second_alert = document.getElementById(secondCommentId);

  if (check_box.checked == true) {
    first_alert.style.visibility = "visible";
    first_alert.style.display = "inline-block";
    second_alert.style.visibility = "hidden";
    second_alert.style.display = "none";
  } else {
    first_alert.style.visibility = "hidden";
    first_alert.style.display = "none";
    second_alert.style.visibility = "visible";
    second_alert.style.display = "inline-block";
  }
}

Here is my working JSFiddle

Again, this is what I'm trying to do:

  • based on the check-box checked/unchecked, the proper message should be displaying on the page when it loads
  • then if the check-box's option is changed, it should display appropriate message

I hope I am making sense (English is not my first language)! Also I'm sorry if it's duplicate question, in that case please provide the URL of the Q/A(s).

Thank you in advance for your time and help!!

AlwaysANovice
  • 983
  • 4
  • 15
  • 34

2 Answers2

1

Your code is working mate , just you have to replace the event on check-box from click to change . Since change event is triggered when you checked / unchked on checkbox.

<input type="checkbox" id="config-email"
    onchange="ToggleSwitchMessage('config-email', 'enable-msg', 'disable-msg')" checked/>
Rohit Kumar
  • 1,948
  • 1
  • 11
  • 16
0

The problem with this code is that it tries to create the onClick event of the element than onChange. It works fine if you change it to onChange.

<input type="checkbox" id="config-email"
    onChange="ToggleSwitchMessage('config-email', 'enable-msg', 'disable-msg')" checked/>

As per displaying the message on the startup itself, call this function with the same arguments in your document.ready function.

UPDATE to your comment:

The following modified code works perfectly in your fiddle.

$(document).ready(function () {
    $("input#config-email").bootstrapSwitch( {
    onText: 'Enable',
    onColor: 'primary',
    offText: 'Disable',
    offColor: 'warning',
    size: 'normal'
  });

    ToggleSwitchMessage('config-email', 'enable-msg', 'disable-msg')
});
Charlie
  • 22,886
  • 11
  • 59
  • 90
  • I tried to call the function in the `document.ready function` but it's not working. Can you please check what I'm doing wrong? https://jsfiddle.net/hiWorld/a29bc42b/2/ – AlwaysANovice Sep 30 '15 at 05:31