1
<form>
  <div class="content-search-block">
    <div class="search-bar">
      <div class="search-bar">
        <input type="text" class="form-control" id="dname" name="dname" value="" placeholder="Otsi töötajaid">
        <button type="submit" id="submit"   class="icon"><i class="fa fa-search"></i></button>
      </div>
    </div>
  </div>
</form>

So i have this kind of search field. This kind of start on JavaScript file.(yeah in WP) But seems that my script doesn't load on submit button?

jQuery(document).ready(function(){
  jQuery('#submit').click(function(){
    var name = jQuery("#dname").val();
  });
});

I tried even

jQuery(document).ready(function(){
  jQuery('#submit').click(function(){
    var name = jQuery("#dname").val();
    jQuery.ajax({
      url: '/wp-admin/admin-ajax.php',
      type: 'POST',   // Adding Post method
      dataType: 'JSON',// Including ajax file
      data: {'action': 'do_search', 'dname':name}, // Sending data dname to post_word_count function.
      success: function(data){ // Show returned data using the function.
        alert(data);
      }
    });
  });
});

Why? script is enqueued and loaded at the loading on page. Still nada.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Martin Tee
  • 43
  • 1
  • 9

2 Answers2

0

In your code you open three div tags but you closed only Two div tags

Balaraju M
  • 473
  • 1
  • 3
  • 14
0

You need to prevent default form submission:

jQuery('#submit').click(function(e){
  ...
  e.preventDefault();
  // or
  return false;
});

or make it

<button type="button" ...
Igor
  • 15,833
  • 1
  • 27
  • 32