I am trying to add a click event for an item returning from the server via ajax. Apparently, I should bind my js file to my answer instead of my main script. Is this the best practice? Should I create a separate js file to add the event to the returned text?
Example:
My jquery - selectWeek.js
$(document).ready(function(){
var url="sendSchedule.php";
$.post(
url,
{week:input},
function(responseText){
$("#ajax").html(responseText);
},
"html"
);
$("#add").click(function(){
return false;
});
});
My homepage
<script type="text/javascript" src="JS/selectWeek.js"></script>
</HEAD>
<BODY>
//the code that trigger ajax is omitted
<div id=ajax>
//the response text will be inserted here
</div>
</BODY>
</HTML>
server response
// display html (omit)
// Do I have to attach the same js file to let
<script type="text/javascript" src="JS/selectWeek.js"></script>
<form>
<input type='button' id='add' value='add'/>
</form>
I am not sure that I will ask my question. I want to know if events need to be added to the returned text. Should I add a js file link to the returned text or is there a way to do this on the main page. I just want to write my js code on the main page and keep it simple. Thanks for any answer.
source
share