marknorkin
marknorkin

Reputation: 4074

JS prevent firing focusout event if input didn't changed

I have the following input:

<input name="video" value="" type="text">

And attached js event:

input.focusout(function(){
        loadThumbnail();
    });

The problem is it triggers always when focus leaves field. Actually it's goods behavior, but didn't fit my needs, because if user didn't changed the field the event will be triggered and the request will be made on server.

I've tried to replace it with change event, but it doesn't triggers when user clean's field.

So what I need is event that will be triggered after user finished editing the field and detect cases when user cleans field or moves focus to/from it, but don't change anything.

Would you suggest a solution?

Upvotes: 0

Views: 1979

Answers (2)

Muhammad Usman
Muhammad Usman

Reputation: 1362

Try something like this to save the old value and compare it with new value:

var oldVal = ''
input.focusout(function () {
    var newVal = input.val();
    if (oldVal != newVal) {
        oldVal = newVal;
        loadThumbnail();
    }
});

Upvotes: 3

ankur140290
ankur140290

Reputation: 640

Try the below part. You will have to tweak this. I just wrote a raw code for U.

var temp='';
input.focusin(function(){
     temp = input.val();
    });


input.focusout(function(){
        if temp != input.val() then
            loadThumbnail();
    });

Upvotes: 1

Related Questions