Glyn
Glyn

Reputation: 2005

Display an error message in HTML using jQuery

I am trying to display an error message using jQuery. It is not displaying when there is an error. I have taken this from another question and modified it.

The jQuery (contained in an if exception test which is triggered) is:

$("#projectIDSelect").html("Please select a Project ID").addClass("error-msg"); // chained methods

The html is:

<p>
    <label for="projectIDSelect">Project ID<span class="required">*</span></label>

    <!--Place to load the Project Names into for selection -->
    <select class="col-lg-3 col-md-3 col-sm-4 col-xs-6"  style="height:30px; color:black;" id="projectIDSelect" name="projectIDSelect" required>
        <option value="" disabled selected>Select a Project</option>
    </select>
</p>

Also, after this has run the dropdown no longer contains the options (these have been dynamically loaded).

Upvotes: 2

Views: 15756

Answers (3)

Parth Raval
Parth Raval

Reputation: 4453

$(function() {
    if (!$('#projectIDSelect option:selected').val()) {
        $("#divError").html("Please select a Project ID").addClass("error-msg"); // chained methods
    } else {
        $("#divError").html("").removeClass("error-msg");
    }
    $("#projectIDSelect").change(function() {
        if (!$('option:selected').val()) {
            $("#divError").html("Please select a Project ID").addClass("error-msg"); // chained methods
        } else {
            $("#divError").html("").removeClass("error-msg");
        }
    });
});
.error-msg {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
   <label for="projectIDSelect">Project ID<span class="required">*</span></label>
   <!--Place to load the Project Names into for selection -->
   <select class="col-lg-3 col-md-3 col-sm-4 col-xs-6"  style="height:30px; color:black;" id="projectIDSelect" name="projectIDSelect" required>
      <option value=""  selected>Select a Project</option>
      <option value="1" >123</option>
      <option value="2" >321</option>
   </select>
</p>
<div id='divError'></div>

Upvotes: 1

Deepak A
Deepak A

Reputation: 1636

You are selecting id in label instead of for selector ref jQuery selector for the label of a checkbox this helps

$("label[for='projectIDSelect']").html("Please select a Project ID").addClass("error-msg");
.error-msg
{
color:red
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<p>
    <label for="projectIDSelect">Project ID<span class="required">*</span></label>

    <!--Place to load the Project Names into for selection -->
    <select class="col-lg-3 col-md-3 col-sm-4 col-xs-6"  style="height:30px; color:black;" id="projectIDSelect" name="projectIDSelect" required>
        <option value="" disabled selected>Select a Project</option>
    </select>
</p>

Upvotes: 0

Naveed Ramzan
Naveed Ramzan

Reputation: 3593

#projectIDSelect is a combo or drop down box. and in your jquery when you update content using .html it will not show error because its not possible to show html content in a drop down box.

You need to add separate identifier for showing message.

Your code could be:

<p>
    <label for="projectIDSelect">Project ID<span class="required">*</span></label>

    <!--Place to load the Project Names into for selection -->
    <select class="col-lg-3 col-md-3 col-sm-4 col-xs-6"  style="height:30px; color:black;" id="projectIDSelect" name="projectIDSelect" required>
        <option value="" disabled selected>Select a Project</option>
    </select>
    <p id="projectIDSelectError"></p>
</p>

and your jquery could be:

$("#projectIDSelectError").html("Please select a Project ID").addClass("error-msg"); // chained methods

Upvotes: 4

Related Questions