Dayana
Dayana

Reputation: 11

On change the text field alert and not on click of button

There is one create category button and once user click on create account and input text in text field and BLUR there should be alert that "Click save to save categories". if user click on save button alert should not come.

I am trying to use blur function but when user click on save button there is also alert for that , what is the best possible way to ignore alert on click of Save button.

<button type="submit" class="button" >Save</button>

<input type="text" name="category_name">

$("input[name=category_name]").change(function(){    
    alert("Click save to save categories");                                    
});

Upvotes: 1

Views: 1214

Answers (2)

Malitta N
Malitta N

Reputation: 3423

Try this

<script type="text/javascript">

var blurTimeout;

function cancelBlur(){
    clearTimeout(blurTimeout);
}

$(function(){

    $("input[name=category_name]").blur(function(){    
        blurTimeout = setTimeout(function(){
            alert("Click save to save categories");                                    
        }, 200);
    });

});

</script>

<button type="submit" class="button" onclick="cancelBlur();">Save</button>
<input type="text" name="category_name" />

This sets a timer to show the alert message and if the submit button is clicked, it removes the timer, hence preventing the message from appearing.

Make sure to add the onclick="cancelBlur();" to your button

Upvotes: 2

Marius Ilie
Marius Ilie

Reputation: 3323

try that on onClick() to set a variable to true. Then, onBlur() try to trigger the alert after 1 second (or less) with setTimeout() and check for the variable to be false. If it's true, do not trigger the alert. Hope that makes sense

Upvotes: 1

Related Questions