<button id="add_tab">Add Tab</button>
<input id="text-search" type="text"/>
<div id="tabs">
<ul>
<li><a href="#tabs-1" style="padding:0;"><span class="color-bar"> </span><div class="alert-header">Alert #1</br><span class="time-date">5:30pm - 07/11/2014</span></div><span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></a></li>
<li><a href="#tabs-2" style="padding:0;"><span class="color-bar"> </span><div class="alert-header">Alert #2</br><span class="time-date">5:30pm - 07/11/2014</span></div><span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></a></li>
<li><a href="#tabs-3" style="padding:0;"><span class="color-bar"> </span><div class="alert-header">Alert #3</br><span class="time-date">5:30pm - 07/11/2014</span></div><span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></a></li>
</ul>
<div id="tabs-1" style="box-shadow: 10px 10px 50px #888888;">
<p class="alert-text">This one is acknowledged</p>
</div>
<div id="tabs-2" style="box-shadow: 10px 10px 50px #888888;">
<p class="alert-text">This one is cleared</p>
</div>
<div id="tabs-3" style="box-shadow: 10px 10px 50px #888888;">
<p class="alert-text">This one is active</p>
</div>
</div>
I am adding 3 divs statically and remaining are added based on jquery call. The click function I apply for class "alert-header" is working fine for static div's click event but not working for dynamically generated div's.. Please give me solution...
Here is my code:
This is that click event call
$('.alert-header').click(function()
{
alert("Called now");
});
This is where Im adding the dynamic div and also for some static content:
var tabTitle = $( "#tab_title" ),tabContent = $( "#tab_content" ),tabTemplate = "<li><span class='color-bar'> </span><div class='alert-header' ><a style='padding:0.5px' href='<%="#"%>{href}'><%="#"%>{label}</a></br><span class='time-date' style='margin-left:-100%'><%="#"%>{time} - <%="#"%>{date}</span><input id='<%="#"%>alertval' type='hidden' value='<%="#"%>{alertId}'/></div><span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",tabCounter = 4;
//mouse over
var tabs =$( "#tabs" ).tabs({
active: false,
event: "mouseenter",
collapsible: true
}).mouseleave(function () {
$(this).tabs({
active: false
});
});
// modal dialog init: custom buttons and a "close" callback reseting the form inside
var dialog = $( "#dialog" ).dialog({
autoOpen: false,
modal: true,
buttons: {
Add: function() {
addTab();
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
form[ 0 ].reset();
}
});
// addTab form: calls addTab function on submit and closes the dialog
var form = dialog.find( "form" ).submit(function( event ) {
addTab();
dialog.dialog( "close" );
event.preventDefault();
});
// actual addTab function: adds new tab using the input from the form above
function addTab() {
var label = tabTitle.val() || "Tab " + tabCounter,
id = "tabs-" + tabCounter,
li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";
tabs.find( ".ui-tabs-nav" ).append( li );
tabs.append( "<div id='" + id + "' style='box-shadow:10px 10px 50px #888;'><p>" + tabContentHtml + "</p></div>" );
tabs.tabs( "refresh" );
tabCounter++;
}
function addTab(alertId, date, time, status, desc) {
var label = tabTitle.val() || "Alert #" + tabCounter,
id = "tabs-" + tabCounter,
ti = time + "",
dt = date + "",
li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ).replace(/#\{time\}/g,ti).replace(/#\{date\}/g,dt).replace(/#\{alertId\}/g,alertId) ),
tabContentHtml = desc || "Tab " + tabCounter + " content.";
tabs.find( ".ui-tabs-nav" ).append( li );
tabs.append( "<div id='" + id + "' style='box-shadow:10px 10px 50px #888;'><p>" + desc + "</p></div>" );
tabs.tabs( "refresh" );
tabCounter++;
}
Html page
<!-- begin snippet: js hide: false -->