Search code examples
javascriptjquerythisonblur

Getting Blurred Element


I have binded blur on an input. On blur, I want to use $(this) and detect this input. Is there a way to do that?

<input data-john="x" data-doe="false">

$('[data-john=x]:not([data-doe=true])').blur(function() {
    console.log($(this))
})

In this example, $(this) is returning Window, not the blurred element.

Is there a way to get input using $(this)?


Solution

  • You can use $('input') with blur() event and have this or $(this) in your console.log:

    $('input').blur(function() {
      console.log(this);
      console.log($(this).data('john'));
      console.log($(this).data('doe'));
      console.log($(this));
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input data-john="x" data-doe="false">