JQuery Post and Ajax Post

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(){

//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 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 #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 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.

+3
source share
2

jquery live events. dom . , , ajax get/post, div id "mydiv" dom. div. , javascript . .

+1

js , <input type='button' id='add' value='add'/>

$("#add").click(function(){ , #add

+1

Source: https://habr.com/ru/post/1746945/


All Articles