I have some jQuery embedded into HTML but I would like to "extract" jQuery and place it into a separate file. Or at least, move it out of the immediate HTML code. I think doing so will fair better in the long run as far as maintainability goes. I hope I am not mistaken by having this principle (SOC) guiding this particular need for refactoring.
I have a <td>
table cell inside a table that holds value of quantity and then reacts to a double click event, by loading up an "edit" box for the value.
<td id="qty">
<?if (!$this->isAnOrder) { ?>
<script>
$("#qty" ).dblclick(function() {
$.get('edit.php?id=<?=$this->id?>', function( data ) {
$( "#qty" ).html( data );
});
});
</script>
<? } ?>
<?=$this->quantity?>
</td>
Question
How would I separate the concerns of the view (HTML) and responding to UI events on the view (JS)?
Special note: I see that I can move the JS code into a separate JS file, but by doing so I hurt maintainability - how would you be able to tell if this particular <td>
field has some JS acting on it? Namely for me part of maintainability is being able to build a mental model of what the code does easily. Simply moving code off into a separate file might hurt that.
So my question becomes - how to separate concerns and keep maintainability intact, if at all possible?