1

I'm trying to make a message pop up saying all changes have been saved, it works once, but then won't work a 2ed or 3rd time. code is below:

How can i make it work every time it is click?

$(document).ready(function() {
   $(".faChkRnd").on('click', function() {
     $("#result").html('All changes have been saved!');
     $("#result").addClass("alert alert-fmaj");
   });
   $("#result").fadeTo(4000, 500).fadeOut(1000, function() {
     $("#updateunit").alert('close');
   });
   $("#updateunit").submit(function() {
     return false;
   });
});

function clearInput() {
   $("#updateunit :input").each(function() {
     $(this).val('');
   });
}

And also tried this:

$(document).ready(function() {
   $(".faChkRnd").on('click', function() {
     $("#result").html('All changes have been saved!');
     $("#result").addClass("alert alert-fmaj");
   });
   $("#result").fadeTo(4000, 500).fadeOut(1000, function() {
     $("#updateunit").alert('close');
     $("#result").removeClass("alert alert-fmaj"); 
   });
   $("#updateunit").submit(function() {
     return false;
   });
});

function clearInput() {
   $("#updateunit :input").each(function() {
     $(this).val('');
   });
}

FIDDLE

6
  • Your click handler is being called, but you dont seem to reset your #result element (ie remove the alert classes, html, etc) so you wouldn't see a change Commented Jan 19, 2016 at 10:25
  • @Patrick Evans updated answer. Commented Jan 19, 2016 at 10:30
  • Can you make a jsfiddle.net example? Commented Jan 19, 2016 at 10:34
  • @Rejith R Krishnan jsfiddle.net/2xc1dk7e Commented Jan 19, 2016 at 10:40
  • You are doing the fadeIn/Out as soon as the document is ready, not after the click has happened. Move that code into the click handler Commented Jan 19, 2016 at 10:44

1 Answer 1

1

If you reset the changes happen during the first show and hide of the alert div and move the fadeTo code inside the click handler, it will work. See this snippet.

$(document).ready(function() {
   $(".faChkRnd").on('click', function() {
     $("#result").stop().html('').removeClass("alert alert-fmaj"); 
     $("#result").html('All changes have been saved!');
     $("#result").addClass("alert alert-fmaj in")
     		.show().css('opacity',1);
     $("#result").fadeTo(1000, 500).fadeOut(1000, function() {
       //$("#updateunit").alert('close');
       $("#result").html('').removeClass("alert alert-fmaj"); 
     });
   });
   $("#updateunit").submit(function() {
     return false;
   });
});

function clearInput() {
   $("#updateunit :input").each(function() {
     $(this).val('');
   });
}
.faChkRnd{margin-left:25px;}
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<input type="checkbox" class="faChkRnd" name="likebox" id="like"> 
<input type="checkbox" class="faChkRnd" name="likebox" id="like"> 
<input type="checkbox" class="faChkRnd" name="likebox" id="like"> 
<input type="checkbox" class="faChkRnd" name="likebox" id="like"> 

<center><div id="result"></div></center> 

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.