focusout on any TextBox

3.2k Views Asked by At

I want to call a javascript function on focusout of a textbox. I have quite a lot of TextBoxes so to prevent a listener for every TextBox is there any possibility of calling the same javascript method on any textBox focusout passing the value of the Textbox as an parameter?

I want to do this on client side and not on server side.

3

There are 3 best solutions below

0
On

The blur event occurs when the field loses focus.

Try this:

$("input").blur(function(){
 //alert("ok")
});
0
On

Use

$("input").focusout(function(){
  var tBval = $(this).val();
  console.log(tBval);
  
  // OR Call a function passing the value of text box as parameter
  //myFunction(tBval);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />

2
On

With jQuery, it's as simple as

$("input").focusout(function(){
    //Whatever you want
});

As pointed out by Milney, you probably want to interact with that specific textbox. To do this, you'd use "$(this)" as a selector.

$("input").focusout(function(){
    $(this).css("background-color", "beige");
});