Remove div "Father"

Asked

Viewed 1,728 times

-1

I have my following code:

    <div class="row">
        <div class="col-md-10">
            CONTEUDO 1
        </div>
        <div class="col-md-2">
            <a href="#" class="btn btn-primary">Delete</a>
        </div>
    </div>
    <div class="row">
        <div class="col-md-10">
            CONTEUDO 2
        </div>
        <div class="col-md-2">
            <a href="#" class="btn btn-primary">Delete</a>
        </div>
    </div>
    <div class="row">
        <div class="col-md-10">
            CONTEUDO 3
        </div>
        <div class="col-md-2">
            <a href="#" class="btn btn-primary">Delete</a>
        </div>
    </div>

And when I click "Delete" it has to delete the entire div (which contains the class "Row") from its respective content.

How could I do that?

  • 1

    Rod, it’s really you????

  • huh? What do you mean?

1 answer

3


You can do it like this

$(function(){
    $('a.delete').click(function(){ 
        $(this).closest('.row').remove();
        return false;
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="row">
        <div class="col-md-10">
            CONTEUDO 1
        </div>
        <div class="col-md-2">
            <a href="#" class="btn btn-primary delete">Delete</a>
        </div>
    </div>
    <div class="row">
        <div class="col-md-10">
            CONTEUDO 2
        </div>
        <div class="col-md-2">
            <a href="#" class="btn btn-primary delete">Delete</a>
        </div>
    </div>
    <div class="row">
        <div class="col-md-10">
            CONTEUDO 3
        </div>
        <div class="col-md-2">
            <a href="#" class="btn btn-primary delete">Delete</a>
        </div>
    </div>

The function closest() returns the first element that corresponds to the specified selector, starting the search on the current element and going up to find.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.