-3

This code is designed to manage a user form where a person selects their continent from a dropdown list. It uses HTML for the form structure and jQuery (JavaScript library) to add dynamic behavior.

When the user selects a continent, the jQuery script checks which option was chosen. If the selected continent is "Antarctica", a message appears below the dropdown stating "Antarctica has no countries", and the submit button becomes disabled so that the user cannot submit the form. This is because Antarctica does not have any countries, so there is no point in proceeding further.

On the other hand, if the user selects any other continent such as Asia or Europe, the error message is hidden (if previously shown), and the submit button is re-enabled, allowing the user to submit the form. This script ensures that form submissions are only allowed for continents that have valid countries to select from. It helps improve user experience by preventing invalid input early on.

// This script will disable the submit button if Antarctica is selected from the dropdown
$(document).ready(function() {
  $('#continent').on('change', function() {
    const selectedContinent = $(this).val()

    if (selectedContinent === 'Antarctica') {
      $('#error_message').show();
      document.getElementById("submit").disabled = true;
    } else {
      $('#error_message').hide();
      document.getElementById("submit").disabled = false;
    }

  });
});
<!DOCTYPE html>
<html lang="en">

<head>
  <title>How To Disable Button ON Selection In J-Query?</title>
</head>

<body>

  <form method="post">

    <div class="division">
      <div class="division_single">
        <label for="continent">Select your continent<span class="required">*</span> :</label>
        <p id="error_message" style="color: red; display: none;">Antarctica has no countries</p>
        <select id="continent" name="continent">
          <option value="">-- Select Continent --</option>
          <option value="Asia">Asia</option>
          <option value="Africa">Africa</option>
          <option value="North America">North America</option>
          <option value="South America">South America</option>
          <option value="Antarctica">Antarctica</option>
          <option value="Europe">Europe</option>
          <option value="Australia">Australia</option>
        </select>
      </div>
    </div>

    <input id="submit" type="submit" value="Submit" required>

  </form>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

</body>

</html>

New contributor
Ahmad Arshad Fq is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • Welcome to Stack Overflow! In what way is your code not working as expected? Please elaborate on the specific problem you are observing and what debugging you have done. To learn more about this community and how we can help you, please start with the tour and read How to Ask and its linked resources.
    – David
    Commented 16 hours ago
  • What is your question? The code seems to behave as it's supposed to.
    – Barmar
    Commented 14 hours ago
  • This is not a code-sharing site, you don't post questions just to demonstrate how to do something. There are many tutorial sites for that.
    – Barmar
    Commented 14 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.