2

I'm currently working on a client script in jQuery and I'm trying to make it as generic as possible.

I'm looking for a way to select a div with the class ".targets" from a button click.

My markup looks something like this:

<form>
    <!-- Products section -->
    <div class="products">
        <div class="container">
            <div class="form-group">
                <div class="col-md-12">
                    <button type="button" class="btn-add">Add</button>
                </div>
            </div>
            <div class="targets">
                ...
            </div>
        </div>
    </div>
    <!-- Resources section, slightly different markup -->
    <div class="resources">
        <div class="container">
            <br/>
            <div class="form-group">
                <button type="button" class="btn-add">Add</button>
            </div>
            <br/>
            <div class="targets">
                ...
            </div>
        </div>
    </div>
</form>

My jQuery looks like this:

$("form").on("click", "btn-add", function(e){
   var targets = $(this).parent().parent().next();
});

This solution works "somehow", but what if the markup changes?

Is there a way in jQuery to select the closest div with the class ".targets" on button click?

Thanks :)

1
  • 2
    $(this).closest('.containers').find('.targets')? Commented May 24, 2017 at 8:07

4 Answers 4

4

Use (this).closest(".container").find(".targets");

this will select the closest element with the class container and search for an element in that with the class targets

$("form").on("click", ".btn-add", function(e){
   var targets = $(this).closest(".container").find(".targets");
   console.log($(targets).text().trim())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <!-- Products section -->
    <div class="products">
        <div class="container">
            <div class="form-group">
                <div class="col-md-12">
                    <button type="button" class="btn-add">Add</button>
                </div>
            </div>
            <div class="targets">
                target1
            </div>
        </div>
    </div>
    <!-- Resources section, slightly different markup -->
    <div class="resources">
        <div class="container">
            <br/>
            <div class="form-group">
                <button type="button" class="btn-add">Add</button>
            </div>
            <br/>
            <div class="targets">
                target2
            </div>
        </div>
    </div>
</form>

Sign up to request clarification or add additional context in comments.

Comments

0

If you know they will always be within the .products element, you could do:

$(this).closest('.products').find('.targets');

A helpful guide for jQuery traversing methods: http://api.jquery.com/category/traversing/

3 Comments

Would this not only work for the first button? since the second button dont have a parent called products
It would, but the point is to find the closest element that will always be present, no matter what the mark-up will be changed to. I avoided using the .container because in my experience you find that with modern coding techniques, "containers" or "wrappers" aren't needed, and then you end up removing them ;)
If you look at his html, 1 button is inside "products", and one inside "resources". So if he use your code, nothing will happen when i clicks the second button
0

Try this:

$(this).closest('.container').find('.targets')

1 Comment

I dont need copy :)
0

You could use closest() which searches for selector higher up in tree and find() which searches for selector going down the tree

$(this).closest('.container').find('.targets')

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.