Search code examples
jquerywordpressvisual-composer

WordPress jQuery only works every other time with ninja tables search


The title says it all. I'm gathering search criteria and putting into a ninja tables search box. It works perfect the first time then every other. Here's what I have in my Visual composer Raw JS object:

<script type="text/javascript">
jQuery(document).ready(function(e) {

  //click on the first option (images)
  jQuery(document.body).on('click',"#fieldname6_1",function (e) {

    //move the screen to the next option
    jQuery('html, body').animate({
      scrollTop: jQuery("#fieldname8_1").offset().top-120
    }, 2000);

    //first unhighlight the image from all tools
    jQuery(".dfield img").removeClass("select_tool");

    //highlight the image
    jQuery(this).addClass("select_tool");

    buildSearch();

  });


  //click on the second option
  jQuery(document.body).on('change',"#fieldname8_1",function (e) {

    //move the screen to the next option
    jQuery('html, body').animate({
      scrollTop: jQuery("#fieldname9_1").offset().top-120
    }, 2000);
    buildSearch();
  });


  //click on the third option
  jQuery(document.body).on('change',"#fieldname9_1",function (e) {

    //move the screen to the next option
    jQuery('html, body').animate({
      scrollTop: jQuery("#fieldname10_1").offset().top-120
    }, 2000);
    buildSearch();
  });


  //click on the fourth option
  jQuery(document.body).on('change',"#fieldname10_1",function (e) {

    //move the screen to the next option
    jQuery('html, body').animate({
      scrollTop: jQuery("#drive-ends-table").offset().top-120
    }, 2000);
    buildSearch();
  });


function buildSearch() {

  var a=jQuery('input[name="fieldname6_1"]:checked').val();
  if(a==null)
    a="";
  var b=jQuery("#fieldname8_1").val();
  var c=jQuery("#fieldname9_1").val();
  var d=jQuery("#fieldname10_1").val();

  var x=a + " " + b + " " + c + " " + d;

  jQuery(".input-group input").val(x);
  jQuery(".input-group input").focus();
  jQuery('.form-group.footable-filtering-search button:first-child').trigger('click');
}

});
</script>

I've run into this issue before, but always managed to find a way around it. I've also thought about filing the search box with the search criteria and forcing a space bar keypress. Any thought on that? That's it. Any help would be greatly appreciated.


Solution

  • I found a solution! I just told jQuery to put the cursor in the textbox them force a spacebar keypress.

      jQuery(".input-group input").focus();
      jQuery(".input-group input").trigger("keypress").val(function(i,val){return val + ' ';});