Reputation: 120
I have this button in html that calls jquery to confirm a delete
<div class='delete' name='<?php echo $story_id; ?>'>delete book</div>
and this is my jquery file
$(document).ready(function(){
$('.delete').click(function(){
var del_id = $(this).attr('name');
$.confirm({
'type':'POST',
'data':'delete_id='+del_id,
'title' : 'Delete Book Confirmation',
'message' : 'You are about to delete this book. <br />It cannot be restored at a later time! Do you still want to continue?',
'buttons' : {
'Yes' : {
'class' : 'blue',
'action': function(){
document.location.href="sample2.php";
}
},
'No' : {
'class' : 'gray',
}
}
});
});
});
The button should pass the value to jquery and if the yes button in the alert is clicked, the value should subsequently be passed to sample2.php. What should i do? Sorry, i am new with jquery. var del_id = $(this).attr('name');
is not working.
Upvotes: 2
Views: 105
Reputation: 167
The $(this).attr('name') is working fine. Try an alert to print the value.
I think u should remove the data attribute from the $.confirm(). And write like this.
$(document).ready(function(){
$('.delete').click(function(){
var del_id = $(this).attr('name');
$.confirm({
'type':'POST',
'title' : 'Delete Book Confirmation',
'message' : 'You are about to delete this book. <br />It cannot be restored at a later time! Do you still want to continue?',
'buttons' : {
'Yes' : {
'class' : 'blue',
'action': function(){
document.location.href="sample2.php?delete_id="+del_id;
}
},
'No' : {
'class' : 'gray',
}
}
});
});
});'
I think it will work.
Upvotes: 2
Reputation: 40639
Try to change
your div element
to button
like,
<button class='delete' name='<?php echo $story_id; ?>'>delete book</button>
Or use data- attribute
in place of name attribute
like,
<div class='delete' data-name='<?php echo $story_id; ?>'>delete book</div>
Fetch it using data() like,
var del_id=$(this).data('name');
Upvotes: 1
Reputation: 102743
The data
parameter of JQuery.post should be an ordinary Javascript object:
'data': { delete_id: del_id },
Upvotes: 1