0

I want to make sure that the after() executes only after focusout() is processed and not before that in jquery. Following is the code:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <form action="index.php" method="post">
        <label for="lblIosDownload">Todays iOS Downloads</label>
        <input name="txtIosDownloads" type="number" > <br>

        <label for="lblAndroidDownload">Todays Android Downloads</label>
        <input name="txtAndroidDownloads" type="number" ><br>

        <label for="iosSignUps">Today's Ios Sign ups</label>
        <input name="txtIosSignUps" type="number" ><br>

        <label for="lblAndroidSignUps">Todays Android SignUps</label>
        <input name="txtAndroidDownloads" type="number" ><br>

    </form>
    <script>
    $(document).ready(function(){
        $("input[name='txtIosSignUps']").focusout(function(e){
            e.preventDefault();
            var numberOfSignups = $(this).val();
            alert(numberOfSignups);
        }).after("<div style='color:red'>Hi<div>");
    });
    </script>


</body>

masterbinoy
  • 118
  • 11
  • Possibly answered at this answer on how to deal with aynchronous code: https://stackoverflow.com/a/14220323/2869791 You could also look into jQuery .then()/.done() – Sarath Chandra Feb 02 '18 at 11:35

1 Answers1

1

Move the after() call inside the event handler so it doesn't occur on page load and occurs when event occurs

$(document).ready(function(){
    $("input[name='txtIosSignUps']").focusout(function(e){
        e.preventDefault();
        var numberOfSignups = $(this).val();
        alert(numberOfSignups);
        $(this).after("<div style='color:red'>Hi<div>");
    });
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150