Search code examples
ruby-on-railssearchcontrollersubmitfilterrific

Filterrific - changing submit button to have same action as hitting enter


Let me start by saying that my ruby knowledge is very, very limited... :)

On the site I'm working on, it's using Filterrific for a few of the forms. The search input has a search button, and if you enter in a value and click the button, it renders the search results within the same page that the form is on.

For the same form, if you type in a value and hit enter, it appends this to the current URL:

?utf8=✓&filterrific[search_query]=VALUEOFINPUT

I'd like to change the search button to have the same action as if the visitor pressed enter. I attempted to do this via jQuery and couldn't get it to work, so I'm assuming I'll have to do it through the controller file.

To be honest, I'm not sure what exactly to show code-wise, so if you need me to post something to clarify, let me know.


****UPDATE****

@Programmer hit the nail on the head. I had to alter it a bit 'cause what I thought was a button turned out to be a div with a glyphicon. Here's the finished code:

function submitButton(){

      $( "div.glyphicon-search" ).replaceWith( "<button type='submit'  id='filt-submit'><span class='glyphicon glyphicon-search'></span></button>" );

    }

    function enterSubmit(){
      $("#filt-submit").keypress(function(event) {
          if(event.which == 13) {
              Filterrific.submitFilterForm;
          }
      });
    }

    submitButton(function () {
        enterSubmit();
    });

Solution

  • Assuming btnSearch is identifier of your search button, please check if below JQuery code does this.

    $("#btnSearch").keypress(function(event) {
        if(event.which == 13) {
            Filterrific.submitFilterForm;
        }
    });