Select the submit button for the specific form for the click event.

If I have the following view, how can I select the submit button based on the form identifier that will be used for the click event?

<form id="login"> <input type="text" name="email"> <input type="text" name="password"> <input type="submit" name="submit"> </form> 

Something like the following works, but it can't just be input[name=submit] , because there can be more than one on a page.

 $('input[name=submit]').click(function (e) { e.preventDefault(); console.log('clicked'); }); 
+4
source share
3 answers

This will automatically select the form control:

 $('#login :submit').click(...); 

http://api.jquery.com/submit-selector/

Greetings

+5
source

Using

 $('#login input[type="submit"]') 

You can read http://www.w3.org/TR/selectors/ for all CSS selectors. (jQuery supports all default selectors + more http://api.jquery.com/category/selectors/ )

+2
source

Using:

  document.querySelectorAll("input[type=submit]")[0].click(); 
+2
source

Source: https://habr.com/ru/post/1446356/


All Articles