The jQuery.click () event does not fire on the link created with .load ()

The .click() event is .click() if the menu is outside id="content" (static HTML) but does not work when the menu is inside id="content" (dynamic HTML using .load() ).

Press function:

 $(document).ready(function () { $("a.type").click(function () { var type = $(this).data("id"); $('#content').load("content.php?" + type); }); }); 

Link to the header menu (click event works here):

 <li><a data-id="1" class="type">Cars</a></li> <li><a data-id="2" class="type">Houses</a></li> 

.load() fills this (click event does not work here):

 <div id="content"></div> 
+4
source share
1 answer

Change

 $("a.type").click(function 

to

 $("a.type").live('click', function 

Or, instead of living, you can also use $(document).on("click", "a.type", function(){}) if you are using jQuery 1.7+.

+8
source

Source: https://habr.com/ru/post/1387474/


All Articles