Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  3] [ 4]  / answers: 1 / hits: 33276  / 9 Years ago, wed, september 2, 2015, 12:00:00

I know we can bind event to dynamically created elements like below



$('some').on('click','class/id of dynamic ele',function(){});


but how to trigger click event on dynamically created element like i have created new element in dom



<div class=one>M</div>


now how can i $( .one ).trigger( click ); ?


More From » jquery

 Answers
13

but how to trigger click event on dynamically created element like i
have created new element in dom




Try defining <div class=one>M</div> without attributes <div>M</div>; setting attributes at second argument to jQuery( html, attributes ) , utilizing .click()




jQuery( html, attributes )



html



Type: htmlString A string defining a
single, standalone, HTML element (e.g. or ).



attributes



Type: PlainObject An object of attributes, events, and
methods to call on the newly-created element.








Important: If the second argument is passed, the HTML string in the first argument must represent a simple element with no attributes.
As of jQuery 1.4, any event type can be passed in, and the following jQuery methods can be called: val, css, html, text, data,
width, height, or offset.






// create dynamic element
$( <div></div>, {
class: one,
text: abc,
on: {
// attach `click` event to dynamically created element
click: function( event ) {
// Do something
console.log(event.target, this.textContent)
}
}
}).appendTo( body )
// trigger `click` event on dynamically created element
.click()

<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>
</script>




[#65220] Sunday, August 30, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kayden

Total Points: 546
Total Questions: 102
Total Answers: 95

Location: Virgin Islands (U.S.)
Member since Fri, Mar 4, 2022
2 Years ago
;