Skip to content
the poor coder

switch alert

Poor Man
Published
Reading time
3 min read
On this page

Switch Alert#

Switch alert is a pop-up or an alert that appears when a user toggles a switch or checkbox. It can be used to inform the user about the action they are about to perform, or to warn them about the consequences of their action.

Implementation using JavaScript#

To implement switch alert using JavaScript, we can use the alert() method. This method displays an alert box with a message and an OK button. We can use this method to display a message when the user toggles a switch or checkbox.

Text

document.getElementById("mySwitch").addEventListener("change", function() {
  if(this.checked) {
    alert("Switch is On");
  } else {
    alert("Switch is Off");
  }
});
  • getElementById() method is used to get the switch element by its ID.
  • addEventListener() method is used to add an event listener to the switch element.
  • The anonymous function inside the event listener checks if the switch is checked or unchecked and displays a message using the alert() method accordingly.

Implementation using jQuery#

To implement switch alert using jQuery, we can use the alert() function in a similar way as with JavaScript. However, we can also use the .on() method to add an event handler to the switch element.

Text

$("#mySwitch").on("change", function() {
  if($(this).is(":checked")) {
    alert("Switch is On");
  } else {
    alert("Switch is Off");
  }
});
  • $("#mySwitch") selector is used to get the switch element by its ID.
  • .on() method is used to add an event handler to the switch element.
  • The anonymous function inside the event handler checks if the switch is checked or unchecked and displays a message using the alert() function accordingly.
  • $(this).is(":checked") is used to check if the switch is checked or unchecked.

Custom Implementation using CSS and JavaScript#

We can also create a custom switch alert using CSS and JavaScript. This implementation uses a hidden div that appears when the switch is toggled. We can style the div to look like an alert box and display a custom message.

Text

<div class="alert" id="myAlert">
  <p class="message">Switch is On</p>
  <button class="close" onclick="closeAlert()">Close</button>
</div>
Text

.alert {
  display: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
  background-color: #fff;
  border: 1px solid #ccc;
  box-shadow: 0px 2px 4px rgba(0,0,0,0.2);
}

.alert.show {
  display: block;
}

.alert .message {
  margin: 0;
}

.alert .close {
  margin-top: 10px;
  padding: 5px 10px;
  background-color: #ccc;
  border: none;
  border-radius: 3px;
}

.alert .close:focus {
  outline: none;
}
Text

document.getElementById("mySwitch").addEventListener("change", function() {
  var alertBox = document.getElementById("myAlert");
  var messageBox = alertBox.querySelector(".message");
  
  if(this.checked) {
    messageBox.innerHTML = "Switch is On";
    alertBox.classList.add("show");
  } else {
    messageBox.innerHTML = "Switch is Off";
    alertBox.classList.add("show");
  }
});

function closeAlert() {
  var alertBox = document.getElementById("myAlert");
  
  alertBox.classList.remove("show");
}
  • The HTML code creates a hidden div with an ID of myAlert. The div contains a paragraph element with a class of message and a button element with a class of close.
  • The CSS code styles the div to look like an alert box.
  • The JavaScript code adds an event listener to the switch element. When the switch is toggled, it updates the message in the div and displays it by adding the show class.
  • The JavaScript code also defines a function to close the alert box when the user clicks the close button.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC