Estoy tratando de hacer clic en un botón ( sbtn2 ) para que se active otro ( sbtn1 ). Lo intento:
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-3.6.0.js"></script> <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script> <script> $(function() { $("#stack").hide(); $("#sbtn2").on("click", function() { $("#sbtn1").click(function() { $("#stack").show(); }); }); }); </script> </head> <body> <button id="sbtn1">OK</button> <button id="sbtn2">See</button> <p id="stack"> Hi, my name is Stack Overflow! </p> </body> </html> Me gustaría que la stack apareciera solo cuando se hace clic en sbtn1 A TRAVÉS sbtn2 .
Es decir, necesito presionar See para que haga clic en OK y aparezca Stack .
Prueba algo como esto:
Agregue detectores de eventos separados a ambos botones
$(function () { $("#stack").hide(); $("#sbtn1").on("click", function () { $("#stack").show(); }); $("#sbtn2").on("click", function () { $("#sbtn1").click(); }); });Actualmente, su código hace que cuando haga clic en el botón 2, se configure un controlador de eventos para el botón 1 para que luego pueda hacer clic en el botón 1 y ver el mensaje oculto.
Para que cuando se haga clic en el botón 2, realice la misma acción que haría el botón 1, simplemente configure un controlador en el botón 2 que llame a la función de devolución de llamada del clic del botón 1.
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-3.6.0.js"></script> <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script> <script> $(function() { $("#stack").hide(); $("#sbtn1").click(function() { $("#stack").show(); }); $("#sbtn2").on("click", function() { $("#sbtn1").click(); }); }); </script> </head> <body> <button id="sbtn1">OK</button> <button id="sbtn2">See</button> <p id="stack"> Hi, my name is Stack Overflow! </p> </body> </html>