Jquery and Ajax Post issue
- by Jerry
Hello guys
I am trying to add a click event on the element that is return from the server via ajax. Apparently, I have to attached my js file inside my return response instead of my main script. Is this the best practice? Do I have to create the separated js file to add event on the return text??
Example:
My Jquery - selectWeek.js
$(document).ready(function(){
//handle ajax..
var url="sendSchedule.php";
$.post(
url,
{week:input},
function(responseText){
$("#ajax").html(responseText);
},
"html"
);
// click on #add button
$("#add").click(function(){
//do something
return false;
});
});
My main page
<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>
response from the server
// display html (omit)
// Do I have to attach the same js file to let #add listen the event?
<script type="text/javascript" src="JS/selectWeek.js"></script>
<form>
<input type='button' id='add' value='add'/>
</form>
I am not sure if i make my question clear. I want to know if I need to add events on the return text. Do I have to add js file link on the return text or there is a way to do it on the main page. Thanks for any reply.