Is this an infinite JS loop?

I have input type = 'text' and using jQuery, every time this text field is blurred, I apply css ('background', 'red') to the text field. The problem is that when I try to debug the puttin code alert('test')and I click OK in the dialog box, the same dialog box appears with the “test”.

Is it just because clicking on OK is considered blurry from the text box or is it something more serious? Thanks in advance.

$('#input').blur(function(){
       $(this).css('background','red');
       alert('test');
}
+3
source share
5 answers

You should debug the console instead of warnings:

$('#input').blur(function(){
       $(this).css('background','red');
       console.log('test');
});

I believe that in most browsers, F12 will display the console.

+3
source

This is not an endless cycle. Closing an alert simply sends an event.

console.log("test") .

.

+2

$('#input').blur(function(){
       $(this).css('background','red');
       console.log('test');
});
+2

.

If you get the blur event again and again, it looks like you are focusing the element again on the blur.

+2
source

Error in code: );missing

$('#input').blur(function(){
       $(this).css('background','red');
       alert('test');
});

Your code works well.

+1
source

All Articles