0

I am using the following code in html:

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>

My JS:

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

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.

1
  • 1
    how do I select only for id="text1" Commented Aug 2, 2018 at 5:06

4 Answers 4

3

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>

Sign up to request clarification or add additional context in comments.

7 Comments

also input event can be used
Thanks for your quick and accurate answer
happy to help you :)
One more, how can I reset the result if I delete the text in the textbox?
@LaishramPilot, i have updated my code to reset result if all value deleted
|
2

use oninput event in text field.

<input type="text" oninput="scriptName">

Comments

0

You can use keyup or keydown event to perform oprations. for example :

$(".className").on("keyup",function(){
   //Code goes here
 });

Comments

0

Here is HTML

<input class="form-control" placeholder="Search Service" id="search_service" name="service" autocomplete="off" value="" type="text">

This is JS Code

jQuery( "#search_service" ).keyup( function() {

var search_ser = jQuery(this).val();

var ajax_url = jQuery('#ajax_url_input').val();

jQuery.ajax({

type:'POST',
url: ajax_url,
data: {
    action: 'search_service',
    search_ser: search_ser,
},
beforeSend: function(){
},
success:function(data){

    jQuery("._services_list").html(data);

}

});

});

Here is my Ajax Function in function.php

/* Ajax Function to Search Service. */

function search_service() {

$search_ser = $_POST['search_ser'];

echo $search_ser;

die();

}

add_action('wp_ajax_search_service', 'search_service');

add_action('wp_ajax_nopriv_search_service', 'search_service');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.