Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from a text field when user finish typing or goes out of textfield using jQuery?

I have a form like;

<form method="post" action="test.php">
 <input type="text" name="field1" class="class1"><span class="info">
 <input type="text" name="field2" class="class1"><span class="info">
 <input type="text" name="field3" class="class1"><span class="info">
</form>

I want to update the corresponding info span when user finish typing or when cursor goes out of form field. keypress() function + setTimeout + clearTimeout will work in the case of typing, but fails in the case of Ctrl+V, or some other method, where keypress is not relevant. The input data is passed into an ajax request like;

$.ajax({
  type: 'POST',
  url: 'ajaxhandler.php',
  data: 'item='+fieldvalue,//class1 values. field1 value, field2 value etc
  cache: false,
  success: function(result) {
    if(result == "true"){
      //update span with true
    }else{
      //update span with false
    }
  }
});

I am trying to achieve this. After user provide input to form field, it may pass to ajax and the corresponding info span may be updated with the ajax reply, either true or false. How can I do this?

like image 350
Alfred Avatar asked Nov 29 '25 19:11

Alfred


1 Answers

you could use the change event like so

http://jsfiddle.net/kasperfish/Zxn3Q/1/

$('input[type=text]').on('change', function(){
  var input=$(this);
  //do your ajax call here
  alert('this can be an ajax call');
  input.next('span.info').html(input.val());
});
like image 96
kasper Taeymans Avatar answered Dec 02 '25 08:12

kasper Taeymans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!