Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to show a message on change event through JS. first time it works but for the second time it doesn't appear . I checked it in debugging mode Change event occurs but the message doesn't appear.


What I have tried:

My code in HTML:

<div class="row">
    <div class="col-md-8 offset-sm-1 showMsg">
        <label id="msg"></label>
    </div>
</div>

<div class="col-lg-12 col-xl-8 col-md-8  col-sm-8 col-xs-12">
                            @Html.DropDownListFor(x => x.rat_starting, list, "--Select List--", new { @class = "form-control", @id = "rat_starting" })
                            @Html.ValidationMessageFor(model => model.rat_starting, "", new { @class = "text-danger" })
                        </div>


<div class="col-lg-12 col-xl-8 col-md-8  col-sm-8 col-xs-12">
                            @Html.DropDownListFor(x => x.rat_ending, list, "--Select List--", new { @class = "form-control", @id = "rat_ending" })
                            @Html.ValidationMessageFor(model => model.rat_ending, "", new { @class = "text-danger" })
                        </div>


My code in JS:

$('#rat_ending').on('change', function () {
    var start = $('#rat_starting').val();
    var end = $('#rat_ending').val();//data("kendoDropDownList").value();
    if (end <= start) {
        Alertmessage();
    }
});

function Alertmessage()
{
    $('.showMsg').hide();
    $('#msg').empty();
    $('#msg').html('End Index Must be greater than Start Index.');
    $("#msg").css("color", "red");
    $('.showMsg').show();
    $('#msg').fadeOut(5000);
}
Posted
Updated 31-Jan-20 1:14am
v2
Comments
F-ES Sitecore 31-Jan-20 5:38am    
Where is the element with an id of "msg"?
Axxiiim 31-Jan-20 6:26am    
code updated please have a look

1 solution

When the msg has finished the fadeOut animation it is hidden, and there isn't anything in your code to show it again, so it remains hidden. "fadeOut" just animates from the current visibility to not shown, but if it is already not shown then it has nothing to do, it remains not shown.

The update below stops the animation if it is currently running (stop), resets the opacity (fadeIn) then starts the fadeOut again.

JavaScript
function Alertmessage()
{
    $('.showMsg').hide();
    $('#msg').empty();
    $('#msg').html('End Index Must be greater than Start Index.');
    $("#msg").css("color", "red");
    $('#msg').stop();
    $('#msg').fadeIn();
    $('.showMsg').show();
    $('#msg').fadeOut(5000);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900