Is this what you are trying to do? Notice I put $.on() in the parent, but choose .button for the action.
.on (events [, selector] [, data], handler (eventObject))
selector A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event always fires when it reaches the selected item.
http://api.jquery.com/on/
<div id="stuff"> <button class="button">Click me!</button> <p>Stuff</p> </div> var $stuff = $('#stuff'), ajaxContent = $stuff.html(); $stuff.on('click', '.button', function(){ $.get('/echo/html/', function(){ $stuff.empty(); console.log($stuff.html()); alert($stuff.html());
http://jsfiddle.net/62uSU/1
Another demo:
var $stuff = $('#stuff'), ajaxContent = $stuff.html(), $ajaxContent, colors = ['blue','green','red'], color = 0; $stuff.on('click', '.button', function(){ $.get('/echo/html/', function(){ color++; if (color == colors.length) color = 0; console.log($stuff.html()); alert($stuff.html()); $ajaxContent = $(ajaxContent); $stuff.append($ajaxContent).css('color', colors[color]); console.log($stuff.html()); }); });
http://jsfiddle.net/62uSU/2/
Jared Farrish Feb 18 2018-12-18T00: 00Z
source share