There are several buttons:

<button id="49btn" class="b1" value="open" onclick="JavaScript:return Validator();">Start</button> <button id="50btn" class="b1" value="open" onclick="JavaScript:return Validator();">Stop</button> <button id="51btn" class="b2" value="open" onclick="JavaScript:return Validator();">Test</button> 

There is JavaScript that displays a warning when a button is clicked, if OK - to perform an action with a button, if Cancel - not to perform operations with a button click:

 $(document).ready(function(){ $("#51btn").click(function(){ $.ajax({ type: 'GET', url: 'runner.php', data:{'51btn':'0'}, success: function(data) { $("p").html(data); } }); }); }); function Validator(){ if(confirm("Вы подтверждаете операцию?") ){ document.getElementById('id').submit(); return(true); }else{ return(false); } } 

How to make this warning work for all buttons that have the id value? Ie not to write a function for each button separately. When you press the SPECIFIC button with such an id, a warning should be issued and if OK is pressed in the warning, then the Validator function for a specific button should be executed (ie, only the button that was clicked and OK was pressed in the warning)

  • Can. And what will it look like? - Dima Kuzmin

2 answers 2

It seems like you can do this (passing id to a function)

 function Validator(id_){ if(confirm("Вы подтверждаете операцию?") ){ document.getElementById(id_).submit(); return(true); }else{ return(false); } } 

and

 "JavaScript:return Validator(this.id);" 

Example (if you confirm the alert with the id button, otherwise alert("2") ):

 function Validator(id_){ if(confirm("Вы подтверждаете операцию?") ){ var ttt= document.getElementById(id_); alert(ttt.id); return(true); }else{ alert("2"); return(false); } } 
 <button id="49btn" class="b1" value="open" onclick="JavaScript:return Validator(this.id);">Start</button> <button id="50btn" class="b1" value="open" onclick="JavaScript:return Validator(this.id);">Stop</button> <button id="51btn" class="b2" value="open" onclick="JavaScript:return Validator(this.id);">Test</button> 

  • And how with this approach do $ (document) .ready (function () proceed? It should be executed after pressing the confirmation. - Dima Kuzmin
  • All figured out. Thank. - Dima Kuzmin

You can handle the click event for the block in which the buttons are located. In this case, adding other buttons will not need to write handlers for them.

 wrapper.addEventListener('click', function(event){ //проверка ситуаций, когда id не заполнено, нет параметра id или клик идёт не на кнопку if (!event.target.id || event.target.id == '' || event.target.tagName != 'BUTTON') return false; if (confirm("Вы подтверждаете операцию?")){ alert (event.target.id); } else { alert ('Выполнение отменено'); return false; } }); 
 <div id='wrapper'> <button id="49btn" class="b1" value="open">Start</button> <button id="" class="b1" value="open">Stop</button> <button id="51btn" class="b2" value="open">Test</button> <div id='56btn'>Не кнопка</div> </div>