I am using JQuery UI tabs to load content through Ajax. I have two tabs that should load html content and execute a script to hide or show some elements in the loaded content. I was running into an issue where script that is returned from Ajaxified content not executing when second tab is clicked. Script is executing when the first tab clicked.
I have code snippet here to reproduce the problem. Can this be fixed?
Here is the HTML page has tab listing
<html lang="en">
<head>
<meta charset="utf-8">
<title>tabs demo</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<div id="tabs">
<ul>
<li><a href="tabContent.html"><span>One</span></a></li>
<li><a href="tabContent.html"><span>Two</span></a></li>
</ul>
</div>
<script>
$("#tabs").tabs();
</script>
</body>
</html>
Tab content html file tabContent.html
<script>
$(document).ready(function () {
$("#HideShowMe").attr("disabled", true);
$("#HideShowMe").hide();
$('#Cancel').click(function () {
$('#HideShowMe').attr('disabled', true);
$('#HideShowMe').hide();
});
$('#Show').click(function () {
$('#HideShowMe').attr('disabled', false);
$('#HideShowMe').show();
});
});
</script>
<h2>Ajxified Content</h2>
<div id="ActionHideShow">
<input type="button" id="Cancel" name="command" value="Cancel" />
<input type="button" id="Show" name="Show" value="Show" />
</div>
<div id="HideShowMe">
<p>Hello there I should hide at any cost</p>
</div>
You're running into a case of duplicate IDs! ID attributes should be unique in any given document.