I'm trying to separate html and jQuery events handlers in my webapp. Here's my context : my webapp works with tabs with ajax loaded content. Each tab content is identified by an unique id. In each tab I got a table. Two or more tabs contents can be the same (like in Chrome for an example, the same website can be loaded in two tabs). In a tab, each table row is editable by clicking a jQuery initialized button.
Here is a simplified example how it currently works :
SMARTY TEMPLATE
<div id="tabContent_{$uniqueId}">
<table>
{foreach $operations as $op}
<tr><td><button id="buttonEditOperation_${op}" /></td></tr>
{/foreach}
</table>
</div>
<script>
$('#tabContent_{$uniqueId} [id^="buttonEditOperation_"]')
.click( function() { alert('CLICK !'); })
.button(blabla);
</script>
When I load a new tab with the same content, only buttons in this new content are initialized, because of #tabContent_{$uniqueId}
selector
I would like to create a javascript file to separate JS and my SMARTY TEMPLATE but I don't know how to select only the content of the new loaded tab. I would like something like this :
SMARTY TEMPLATE
<script type="text/javascript" src="./js/buttonInit.js"></script>
<div id="tabContent_{$uniqueId}">
<table>
{foreach $operations as $op}
<tr><td><button id="buttonEditOperation_${op}" /></td></tr>
{/foreach}
</table>
</div>
JS FILE
<script>
$('#tabContent_?????? [id^="buttonEditOperation_"]') // HERE IS MY PROBLEM
.click( function() { alert('CLICK !'); })
.button(blabla);
</script>
I have some ideas (HTML5 data properties, initialization function, global variable, etc.) but I would like to know the best solution for this problem.
Thanks for your feedback !