I have a foreach loop that displays a list of To-do items that it gets from a database I've created. Here's how I do that:
<ul class="todo-list">
<?php foreach($todos as $todo) { ?>
<li class="todo">
<button class="delete-todo">×</button>
<input type="checkbox" class="toggle-checked">
<span id="todo-text"><?php echo $todo; ?></span>
</li>
<?php } ?>
</ul>
I have an X button that I want to be able to delete the to-do item from that specific li from my database, but I'm having trouble getting the value of what is in the span tag.
Here's some sample JQuery that isn't working right:
$('.delete-todo').on('click', function(e){
$.ajax({
type: "POST",
url: "deleteTodo.php",
data: {"deleteTodo": $(".todo-text").next().text()},
async: false,
success: function(){
},
error: function() {
}
});
});
What is the best way of getting the value from the span tag? Any help is greatly appreciated!
Edit:
I've now switched it so that each todo has a unique id and I use the id to remove from the database. Thanks for the help!
idshould be uniqueidin your database and not the actual text, what if you have two listings forGet Milk?