JQuery or JavaScript to alert on button click if selection not made

jquery on click
jquery trigger click
jquery click not working
jquery onclick call function
document on click jquery
jquery ::after click event
jquery selector
jquery button

I have a button that when clicked points you to another html page. I am stuck currently on a click event that I am not sure to use javascript or jquery on. Pretty much, when I click and there is no item in the select tag selected, then show the alert pop up. Else, proceed to the normal navigation to the next page. Any feedback is appreciated.

$(document).ready(function() {
  $("#showDangerAlert").click(function() { 
    $(this).show(); $(this).hide(); 
  });
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="showDangerAlert" class="alert alert-danger" role="alert" style="text-align: center">
    You have not selected a Delivery Option! Please choose one to proceed.
</div>

<select id="colorselector" class="custom-select d-block mb-3">
  <option value="red">Select...</option>
  <option id="shipToAddress" value="yellow">Ship to Address</option>
  <option id="inStorePickup" value="blue">In-Store Pickup</option>
</select>

<div id="yellow" class="colors">
  <a href="shoppingCart.html" class="btn btn-primary mx-auto mb-3 cartLink">
    Add to Cart 
    <span class="fa fa-shopping-cart"></span>
  </a>
</div>
<div id="blue" class="colors">
  <small class="mb-3">You will select your desired store during   
    <strong>checkout.</strong>
    <span> 
      <a href="#" data-target="#storeListing" data-toggle="modal"> 
        <span class="fa fa-map-marker"></span> 
        See available stores near you.          
      </a>
    </span>
    <a id="inStorePickupRedirect" href="shoppingCart_inStorePickup.html" class="btn btn-primary mx-auto my-3 cartLink">
      Add to Cart 
      <span class="fa fa-shopping-cart"></span>
    </a>
  </small>
</div>

If there is no option selected then this will alert no option selected else you can do whatever you want. This same code can work with JavaScript, just need to change selectors.

$("#btncheck").on("click",function(){
      if($("#mySelect")[0].selectedIndex <= 0){
        alert("No Option Selected")
      } else {
        alert("Option Selected")
      }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select id="mySelect">
      <option>Select</option>
      <option value="1">First</option>
      <option value="2">Second</option>
      <option value="3">Third</option>
      <option value="4">Fourth</option>
    </select>
    <input type="button" id="btncheck" value="check"/>

How to check if a button is clicked or not in jQuery, How do you check a button is clicked or not in jQuery? The jQuery alert message is one of the best way to display notification to the users. While writing any code, you have to use alert messages on click of the button or occuring of some event. What is jQuery Alert Message. An alert message is a notification or information which you can provide to the user on occuring of some event.

$(document).ready(function() {
  $('#showDangerAlert').show();
  $("#colorselector").change(function() { 
  var sel = $("#colorselector option:selected").val();
    if(sel=='red') {$('#showDangerAlert').show();} else {$('#showDangerAlert').hide();}
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="showDangerAlert" class="alert alert-danger" role="alert" style="text-align: center">
    You have not selected a Delivery Option! Please choose one to proceed.
</div>

<select id="colorselector" class="custom-select d-block mb-3">
  <option value="red">Select...</option>
  <option id="shipToAddress" value="yellow">Ship to Address</option>
  <option id="inStorePickup" value="blue">In-Store Pickup</option>
</select>

<div id="yellow" class="colors">
  <a href="shoppingCart.html" class="btn btn-primary mx-auto mb-3 cartLink">
    Add to Cart 
    <span class="fa fa-shopping-cart"></span>
  </a>
</div>
<div id="blue" class="colors">
  <small class="mb-3">You will select your desired store during   
    <strong>checkout.</strong>
    <span> 
      <a href="#" data-target="#storeListing" data-toggle="modal"> 
        <span class="fa fa-map-marker"></span> 
        See available stores near you.          
      </a>
    </span>
    <a id="inStorePickupRedirect" href="shoppingCart_inStorePickup.html" class="btn btn-primary mx-auto my-3 cartLink">
      Add to Cart 
      <span class="fa fa-shopping-cart"></span>
    </a>
  </small>
</div>

Fix: jQuery .click() Event Not Working with Dynamically Added , raised from an element known as btnNew wherever it is on the body. An alert box is mainly used to share or input information with users. JavaScript Alert box will pop up on the website when some action or interaction happen

You can attach an click event using JQuery and check if the user has done some selection to the select. If user has select something do the navigation using javascript.

$('#button').on('click', function (e) {
    if ($('#selector_id').val() != no_selected_value) {
       window.location = "/your_page";
    } else {
       $('#showDangerAlert').show();
    }
});

So no_selected_value in your case should be red/yellow or blue.


EDIT

I've done some changes in your code snipped to work as I've explained above.

$(document).ready(function() {
  $("#showDangerAlert").hide();
  $("#showDangerAlert").click(function() { 
    $(this).hide(); 
  });
});

$('#button').on('click', function (e) {
    if ($('#colorselector').val() != 'red') {
       window.location = "/shoppingCart.html";
    } else {
       $('#showDangerAlert').show();
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="showDangerAlert" class="alert alert-danger" role="alert" style="text-align: center">
    You have not selected a Delivery Option! Please choose one to proceed.
</div>

<select id="colorselector" class="custom-select d-block mb-3">
  <option value="red">Select...</option>
  <option id="shipToAddress" value="yellow">Ship to Address</option>
  <option id="inStorePickup" value="blue">In-Store Pickup</option>
</select>

<div id="yellow" class="colors">
  <button id="button" class="btn btn-primary mx-auto mb-3 cartLink">
    Add to Cart 
    <span class="fa fa-shopping-cart"></span>
  </button>
</div>
<div id="blue" class="colors">
  <small class="mb-3">You will select your desired store during   
    <strong>checkout.</strong>
    <span> 
      <a href="#" data-target="#storeListing" data-toggle="modal"> 
        <span class="fa fa-map-marker"></span> 
        See available stores near you.          
      </a>
    </span>
    <a id="inStorePickupRedirect" href="shoppingCart_inStorePickup.html" class="btn btn-primary mx-auto my-3 cartLink">
      Add to Cart 
      <span class="fa fa-shopping-cart"></span>
    </a>
  </small>
</div>

.select(), How can write Click event for dynamically generated button in jQuery? This can be done in multiple ways.One simple way is to declare a flag and set the flag value to true on button click. Based on this value it can be understood if the button was prevously clicked or not.Suppose the button html tag is as [code]&lt;inpu

.click(), Description: Bind an event handler to the "select" JavaScript event, or trigger that Merely setting the location of the insertion point will not trigger the event. After this code executes, clicks on the Trigger button will also alert the message:. Using the select () method of the HTML DOM Input Text Object to select some content of a text field. When this happens, the onselect event fires, which will trigger an alert function. // Select the contents of a text field. function mySelectFunction () { document.getElementById("myText").select(); // Alert some text when the text in the text

.one(), Description: Bind an event handler to the "click" JavaScript event, or trigger that event on an element. Now if we click on this element, the alert is displayed:. In this post, we’ll show you how to implement a simple Alert popup window which you can use instead of the standard JavaScript alert. 1.

.change(), A string containing one or more JavaScript event types, such as "click" or A selector string to filter the descendants of the selected elements that trigger the After the code is executed, a click on the element with ID foo will display the alert. i to alert on click event of anchor tag using jquery my code is: <a href="#" class="order"><img src="image/payment.png" width=";20&quot; height=&quot;20&q

Comments
  • Can you show what you've tried so we can help you?
  • $(document).ready(function() { $("#showDangerAlert").click(function() { $(this).show(); $(this).hide(); }); });
  • I deleted other attempts, sorry. But all I wanted to do is to evaluate the selection and if none are selected, show the error, else if one is selected, then proceed as normal
  • $(this).show(); $(this).hide();??
  • @Jeto I deleted other attempts without saving. I am just seeking direction on this.