How to find out if a certain button has been pressed, as in the checkboxes

eg

if()// button1 is clicked { alert("Button 1 was clicked"); } if()// button2 is clicked { alert("Button 2 was clicked"); } 
+4
source share
5 answers
 //you can change type to submit if its a form submit button $("input[type='button']").click(function(){ alert("button with the id: "+$(this).attr("id")+" was clicked!"); }); 

The button identifier can be used as an identifier.

+3
source
 jQuery('#button1, #button2').click(function () { if (this.id == 'button1') { alert('Button 1 was clicked'); } else if (this.id == 'button2') { alert('Button 2 was clicked'); } }); 
+3
source

I assume in your answer that you are looking to see if the checkbox was checked and not clicked. It would seem that this is the most logical conclusion from your question. Let me know if this is not true.

Use . is () function

 if ($("#Button1").is(":checked")){ alert("Button 1 was clicked"); } if ($("#Button2").is(":checked")){ alert("Button 2 was clicked"); } 
+1
source
 $('#button1').click(function() { alert('Button 1 Clicked'); } 
0
source
 $(":button").click(function(){ var $id=$(this).attr("id"); alert("Button"+ id); }); 

it will work for each button type input element

0
source

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


All Articles