0

I'm trying to add a click handler to an li that is added to the DOM through jQuery. I thought this is the way you did it:

jQuery('.deleteUpload').on('click', '.deleteUpload', function() {
    var strDocId = jQuery(this).prop('id');
    //if (confirm('Are you sure you want to delete this resource') == true) {
        $.post('/study-clubs/meetings/delete-upload', {strDocId : strDocId},
            function(objData){
                console.log(objData);
            }
        );
        jQuery('li#' + strDocId).remove();
    //}

    return false;
});

Here is my markup:

<li class="pdf deleteLi" id="<?php echo $this->escape($this->objDocRow->id); ?>">
   <label><input type="radio" name="documents[1]" value="0" /> Pre-Meeting</label> &nbsp;
   <label><input type="radio" name="documents[1]" value="1" /> Post-Meeting</label> &nbsp;
   <label><input type="radio" name="documents[1]" value="2" checked /> Do Not Use</label> &nbsp;
   <a href="#" class="deleteUpload" id="2">Delete</a> &nbsp;
   <a href="/..."><?php echo substr($this->escape($this->objDocRow->doc_title), 0, -4); ?></a>
</li>

I want to delete the li when you click on the a tag with a class of deleteUpload. What am I doing wrong? When I click on it, I'm taken to the top of the page. I also tried deleteLi instead of deleteUpload in the jQuery, but that didn't work either.

5
  • 1
    Try jQuery(document).on('click', '.deleteUpload', function() { Commented May 9, 2013 at 21:21
  • You can use .bind() and .unBind() api.jquery.com/bind to add/remove click handlers. Commented May 9, 2013 at 21:21
  • @ChristopherMarshall: on() is the new, cooler way to attach event handlers these days. bind() will soon join the deprecated methods like live() ;). Commented May 9, 2013 at 21:23
  • Thanks, jtheman. I knoew there was something I was forgetting. Commented May 9, 2013 at 21:24
  • 1
    I really dig .on() @Matt, Thanks for the heads up on deprecation! Commented May 9, 2013 at 21:26

2 Answers 2

2

Try changing the first line to:

jQuery('.deleteLi').on('click', '.deleteUpload', function() {
Sign up to request clarification or add additional context in comments.

Comments

1

There's no need to have the second selector. With it jquery will look for a child of .deleteUpload that has the class deleteUpload.

jQuery('.deleteUpload').on('click', function() {
    var strDocId = jQuery(this).prop('id');
    //if (confirm('Are you sure you want to delete this resource') == true) {
        $.post('/study-clubs/meetings/delete-upload', {strDocId : strDocId},
            function(objData){
                console.log(objData);
            }
        );
        jQuery('li#' + strDocId).remove();
    //}

    return false;
});

Comments

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.