0

I have this link:

<a id='delete' href='/add/delete.php?iid=XXXXX'>#</a>

And I want to pass it on through Jquery so the page doesn't refresh. However, I'm having a difficult time making that happen. All of the click events and ajax I've tried with Jquery have been unsuccessful.

Basically, I want the ajax to send delete.php the value in the iid parameter.

1

2 Answers 2

0

Here try this the href='1' is the id you want to delete, also to replace the text it finds the id="d1":

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
  $(".delete").click(function(e){
    var del_id = $(this).attr('href');
    e.preventDefault();
    $.post("/add/delete.php", { iid: del_id } ,function(data){
        //change the link to deleted
        $('#d'+del_id).replaceWith('<a href="#">Deleted</a>');
    });
  });
});

</script>
</head>
<body>
 <a class='delete' id="d1" href='1'>Delete</a>
 <a class='delete' id="d2" href='2'>Delete</a>
 <a class='delete' id="d3" href='3'>Delete</a>
 <a class='delete' id="d4" href='4'>Delete</a>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

That works great! Thanks so much for the help. One final problem I'm having is that I want to remove the parent li element and all of its content. When I use $(this).closest('li').remove(); in the area you put the replaceWith code, nothing happens. Is that the wrong syntax? Below is my HTML code: <li> <div class='singleitem'> <img src='/css/images/image.jpg' width='30' height='30' /><div id='listtext'><h1>title</h1></div> <a class='delete' id='$iid' href='#'></a> <div id='helper'></div> </div> </li>
0
$(function(){
  $("#delete").click(function(e){
    e.preventDefault();
    $.post("../add/delete.php", { iid: "xxx" } ,function(data){
       //do whatever with the result which is in the variable data.
       alert(data)
    });
  });
});

Since it is a delete, use POST. Otherwise some crawlers will crawl your delete page and you will see your tables are empty one fine morning.

EDIT : You can associate the value to be passed with the anchor tag in someway. Ex :Change your id tag to be in this format del-YouIdTobe deleted ( Ex : del-1,del-2 etc..)

<a id='delete' href='#' id="del-1>Delte 1 </a>
<a id='delete' href='#' id="del-2>Delte 2 </a>
<a id='delete' href='#' id="del-3>Delte 3 </a>

Script

$(function(){
  $("#delete").click(function(e){
    e.preventDefault();
    var val=$(this).attr("id").split("-")[0];
    $.post("../add/delete.php", { iid: val } ,function(data){
       //do whatever with the result which is in the variable data.
       alert(data)
    });
  });
});

1 Comment

Thanks for the quick reply! Since the XXXXX part of the parameter is changing from link to link, is there a way with Jquery to pull that parameter out of the link to pass along?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.