0

With the following javascript I'm trying to submit a result once it is selected, but nothing happens! Any ideas why this is happening?

<input name="search" id="search" class="search-query search_size" placeholder="Enter keyword to search" data-autocomplete-label="Sorry, nothing found." data-autocomplete="/items/autocomplete_item_title" type="search">

<script>
$('#search').bind('railsAutocomplete.select', function(event, data){
    $('#search').submit();
    alert(data.item.id);
});
</script>

Update 1

This is the rails form I'm trying to submit:

<%= form_tag items_path, :method => "get", id: "search" do %>
   <%= text_field_tag :search, params[:search], class: "search-query search_size",
                               placeholder: "Enter keyword to search", type: "search",
                               :"data-autocomplete-label" => "Sorry, nothing found.",
                               data: {autocomplete: autocomplete_item_title_items_path } %>
  <%= submit_tag "Search", name: nil, :style => "display: none;" %>
<%end%>
2
  • What do you actually want the submit action to do? Post data to an specific page? Commented Nov 20, 2017 at 16:08
  • @TIm... it's a basic search form in rails. I would like to select the autocomplete result and once selected automatically submit it! hope this helps! Commented Nov 20, 2017 at 16:12

1 Answer 1

4

You can run submit function only on form element or on input[type="submit"]. That's why nothing happen.

It can only be attached to elements. Forms can be submitted either by clicking an explicit , , or , or by pressing Enter when certain form elements have focus.

https://api.jquery.com/submit/#entry-longdesc

on form:

function submit(event) {
  alert('form submitted');
  e.preventDefault();
}

$('#form').submit(() => {
  console.log('form submitted');
  return false;
});

$('#form').submit();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
  <button type="submit">Submit</button>
</form>

on input:

function submit(event) {
  alert('form submitted');
  e.preventDefault();
}

$('#form').submit(() => {
  console.log('form submitted');
  return false;
});

$('button').submit();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
  <button type="submit">Submit</button>
</form>

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

7 Comments

Thanks for the heads up @Mosh Feu! Is there a way around this? I would like to submit for search when a autocomplete result is selected!
Do you have a form that the input inside it?
If I'm not wrong, the form and the input have the same id search right?
Replace one of them. id attribute must be unique. Once you change it, you can call submit to the form element. Just note that it will refresh the page unless the form has handler that prevent the refresh and send the data with ajax, like in this answer.
Thanks for the info @Mosh Feu... I have already tried this, the one id was search and the form id was search-form, but nothing happened again! Not sure what wrong here! Kind of a newbie here
|

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.