I have a search form, and would like it so that when I paste something into the form, it automatically submits the form. I would also like to retain functionality so if I don’t paste something, and rather manually type it, that I have to press enter/search button for it to work.
Here is my form:
JavaScript
x
12
12
1
<h1 class = "CustomerTitle">Search Tool</h1>
2
<%= form_with url: "/support/search_res", class: "form", method: :get do |form| %>
3
<%= form.text_field :query, class: "input", placeholder: "Search for a user..." %>
4
<%= form.submit "Search", class: "submit_button", id: "pasteSubmit", onpaste: 'pasteAndGo()' %>
5
<% end %>
6
7
<script>
8
function pasteAndGo() {
9
document.getElementById('pasteSubmit').submit();
10
}
11
</script>
12
Could somebody tell me why this is not working?
Edit: I’ve tried Sercan Tırnavalı’s approach:
JavaScript
1
12
12
1
<h1 class = "CustomerTitle">Revuto Customer Support Tool</h1>
2
<%= form_with url: "/support/search_res", class: "form", onpaste: 'pasteAndGo()', method: :get do |form| %>
3
<%= form.text_field :query, class: "input", placeholder: "Search for a user..." %>
4
<%= form.submit "Search", class: "submit_button" %>
5
<% end %>
6
7
<script>
8
function pasteAndGo() {
9
document.getElementsByClassName('form')[0].submit();
10
}
11
</script>
12
with no progress. Any other ideas/solutions are appreciated.
Advertisement
Answer
I believe you have the onpaste
in the wrong place.
Please try this:
JavaScript
1
12
12
1
<h1 class = "CustomerTitle">Revuto Customer Support Tool</h1>
2
<%= form_with url: "/support/search_res", class: "form", method: :get do |form| %>
3
<%= form.text_field :query, class: "input", placeholder: "Search for a user...", onpaste: 'pasteAndGo()' %>
4
<%= form.submit "Search", class: "submit_button" %>
5
<% end %>
6
7
<script>
8
function pasteAndGo() {
9
document.getElementsByClassName('form')[0].submit();
10
}
11
</script>
12