• 1
Votes
name
name Punditsdkoslkdosdkoskdo

How to trigger an event in input text while typing in php?

I am using the following code in html:

Text 1: <input class="form-control text-right" name="majorhead" id="text1" required="" type="number" /> 
<!-- <button class="my_button" type="button" role="button">Click Me<!--

My JS:

       
    jQuery(".my_button").click(function () {
        jQuery.get(ajaxurl, {'action': 'sayhello'},
                function (msg) {
                    jQuery(".result_area").html(msg);
                });
    });

For now I am able to achieve my result when I click the button. But I want to achieve the same result while typing(my code will need to check for every keys typed) in the input text.

Try this:You can bind keyup event handler to text1 and call your function in it.

 

jQuery(document).ready(function(){
      jQuery("#text1").on("keyup",function(){  
        if($(this).val()) {
           jQuery.get(ajaxurl,{'action': 'sayhello'}, 
             function (msg) { jQuery(".result_area").html(msg);
           });
        } else { // once value deleted from text1, reset the result
          jQuery(".result_area").html('');
        }
      });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Text 1: <input class="form-control text-right" 
        name="majorhead" id="text1" required="" type="number" /> 
<div class="result_area"></div> 
<button class="my_button" type="button" role="button">Click Me</button>
  • 1
Reply Report