There are several buttons created in the loop. There is an event handler, it is necessary that it be triggered by pressing each button and at the same time knowing which button was pressed. How to implement?
- add each block with a button data-attribute and read it. or create input with the parameter hidden and store in it information about the desired button, which is then read as well. - lexxl
|
2 answers
Hang an event on the class. In this case, you will have an object that caused the event:
$('a.clickme').on('click', function() { console.log(this); }); - I now have `$ (document) .ready (function () {$ ('# abtn'). click (function (e) {e.preventDefault ();})}` abtn is an id, I changed it class, how can I write this in the handler? - world XAKER
$(document).ready(function() { $('.abtn').click(function(e) { e.preventDefault(); }) }Only sampling by class alone is a resource-intensive thing; tag, as I wrote above:a.abtn, if the object is a link. - Iaroslav Gashuk- Brrrrrrrrrrrrr ... - Qwertiy ♦
- @Qwertiy if the answer is bad - explain what) - Nick Volynkin ♦
- @NickVolynkin, at least in that it does not work with the dynamically created buttons referred to in the question. Unless, of course, they are created before calling this script, that would be strange. And even if so, then the live-subscription here still seems more logical. - Qwertiy ♦
|
$("div").on('click', "button", function (event) { $(this).css("color", "red"); }); $("div").html(Array(17).join("<button></button>")); body { counter-reset: i; } button { counter-increment: i; } button:after { content: counter(i); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div></div> |