Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  94] [ 1]  / answers: 1 / hits: 15507  / 9 Years ago, wed, july 22, 2015, 12:00:00

I don't understand why this isn't working. I have a table that includes a div and an image in the header. When I click on this, I want to fire the click event via a jQuery function. Here is a screenshot of the HTML:



enter



And here is the jQuery function:



$(document).ready(function () {
console.log('ready');

$('#add_external_link').on('click',function(){
alert('clicked');
});
});


I believe that the element is in the DOM before the event is bound. The tail of the HTML looks like this (it's the 'external_link_dialog.js' file that contains the jQuery language from above):



        <script src=//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js></script>
<script src=//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js></script>
<script src=js/jquery.dlmenu.js></script>
<script src=js/external_link_dialog.js></script>

</body>
</html>


My console output shows ready as soon as the page is loaded. However, when I click that <div>, nothing happens. I see no errors, no console output, and of course no alert. Can anyone see what I am doing wrong? I've been at this for hours, and am out of ideas. Thank you!


More From » jquery

 Answers
472

I guess that you are appending the div after the document.ready .
If you are adding that add_external_link div dynamically, you should attach the .on(click function after appending that div.



If the div is not added dynamically, then try adding a timeout



$(document).ready(function () {
console.log('ready');
setTimeout(function(){
$(document).on('click','#add_external_link',function(){
alert('clicked');
});
},1000);
});

[#65728] Sunday, July 19, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janettejordynm

Total Points: 550
Total Questions: 94
Total Answers: 98

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
janettejordynm questions
Tue, Nov 24, 20, 00:00, 4 Years ago
Sat, May 23, 20, 00:00, 4 Years ago
Mon, Apr 6, 20, 00:00, 4 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
;