How to handle onclick events from multiple dynamically created anchor tags using jquery/javascript? -
I have several dynamically-made anchor tags that work as buttons when any click on any of these buttons If so, I want to call a single Javascript function that sits in a different file, which will do the rest of the work. My problem is that I can not call the function properly through the 'onclic' event, so I am trying to find a way around it, but this is not so easy for me
I have tried many different ways, so I understand some limitations with this approach:
Standard HTML anchor with Javascript:
& lt; A href = "Onclick =" doSomething (parameter1, parameter2, ...); "& Gt; Button & lt; / a & gt; This is not helping because there are synchronization problems and I have found that the href will always be chosen from the top click above If you know any work here, please let me know.
Binding through JQuery:
$ (Document) .ready (function () {$ ("# button_id"). Click (function () {doSomething (parameter1, parameter2, ...);});}); I understand that good for binding To live to work, each button should your ID to make it easier to add them with the binding method. However, I'm not sure how many ids to the .click function, and how it is not given to these parameters (unless I capture them via button ID by calling them other JS functions).
Any suggestions / ideas are highly appreciated!
EDIT: How do I add button_data to the parameter 'doSomething (parameter)'? 'Function?
list_element = '& lt; Span class = "ui-li-count" data-mini = "true" & gt; + Button_data + '& lt; A href = "#" class = "my_button" & gt; Button & lt; / A & gt; & Lt; / Span & gt; Please remember that this data is dynamically acquired from the database and posted on the site for the first time on the site. There is no need to use an id for this
. Which are you giving similar functionality to a group of elements, since this you have to contradict what you need to do. Instead, they should give them a class for the group: & lt; A href = # "class =" my-button "& gt; button & lt; / a & gt;; and javascript:
$ (Document) .ready (function () {$ ('body') .On ('click', '.my-button', function () {doSomething (parameter1, parameter2, ...);}); }); This click event has all the .my-button elements, even people you have not created yet. Change the body with the original element, which includes all the buttons, so that the work is a little faster.
Comments
Post a Comment