I have a table with fields where I can add dynamically table data via JQuery. The problem is the elements I add aren't having the properties I've set for that group of td's until I reload the entire page and I don't know what has to be triggered to take action also for new ones.
here is my code:
#js file
function add_task() {
task_name = $("#task-name").val();
due_date = $("#task-due_date").val();
var new_task = $.ajax({
url: "/add_task/",
data: "task="+task_name+"&due_date="+due_date,
success: function(task_id){
get_task(task_id);
}
});
}
function get_task(task_id) {
$.ajax({
url: "/get_task/"+task_id+"/", //get the html from single_task.html
success: function(result){
$('#container').append(result);
$('#task-'+task_id).fadeIn(500);
}
});
}
#single_task.html
<tr id="task-{{task.id}}">
<td><div id="task">
<a href="#" data-type="text" data-pk="{{task.id}}" data- url="/update_task/{{task.id}}/" data-title="Enter task">{{ task.task }}</a>
</div></td>
<td>{{ task.initialized_at|age }}</td>
<td>{{ task.due_date }}</td>
<td>{{ task.done }}</td>
<td><button onclick="delete_task('{{task.id}}');" class="btn"> X </button></td>
#index.html //place where to load everything
<table id="container" class="table table-bordered">
<th style="width:200px;">task</th>
<th style="width:60px;">started</th>
<th style="width:80px;">due date</th>
<th style="width:50px;">status</th>
<th style="width:20px;">action</th>
{% for task in tasks %}
<tr id="task-{{task.id}}">
<td>
<div id="task"><a href="#" data-type="text" data- pk="{{task.id}}" data-url="{% url 'todo:update_task' task.id %}" data-title="Enter task">{{ task.task }}</a></div>
</td>
<td>{{ task.initialized_at|age }}</td>
<td>
<div id="due_date"><a href="#" data-type="date" data-pk="{{task.id}}" data-url="{% url 'todo:update_task' task.id %}" data-title="New date"> {{ task.due_date }}</a></div>
</td>
<td>{{ task.done }}</td>
<td><button onclick="delete_task('{{task.id}}');" class="btn btn-link"> X </button></td>
</tr>
{% endfor %}
</table>
Your help would be very much appreciated :)
Thank you in advance!