3

I have alot of elements with class of .button all over my layout. Upon a click of the element with class of button, I need to check first that the element is within the parent div of either box1 or box2.

A short example would be... if the clicked button is within the box1 div && (another condition) then do something. Would it be best to use the combination of .closet and .length to check if the button is within a certain div?

<div id="box1">
        <div id="column1">
             <div class="round_button"></div>
             <div style="background: #000">
                  <div class="button"></div>
                  <div class="button"></div>
                  <div class="button"></div>
                  <div class="button"></div>
                  <div class="button"></div>
                  <div class="button"></div>
             </div>
        </div>
 <div> 

<div id="box2">
        <div id="column2">
             <div class="round_button"></div>
             <div style="background: #000">
                  <div class="button"></div>
                  <div class="button"></div>
                  <div class="button"></div>
                  <div class="button"></div>
                  <div class="button"></div>
                  <div class="button"></div>
             </div>
        </div>
 <div> 

5 Answers 5

8

In addition to the answer above, if there are a number of buttons, and instead of binding an event separately to each, you just want to attach once, based upon its ancestor, you can do something like this:

$(".button").click(function() {
  if ($(this).is("#box1 *")) {}
});
Sign up to request clarification or add additional context in comments.

7 Comments

@undefined, I can't find any. Where do you think is the error?
@Cupidvogel There is an extra (.
@user1502434, When the button is clicked, you check whether this button is any one of the descendants of the element with id box1, if so, you take a certain action. This check is done by if (($(this).is("#box1 *")).
great, I think this will be my preferred approach! Very nice.
How does this compare with if($(this).closest("#box1").length) in terms of efficiency?
|
4

If the functionality between the two groups of buttons differs significantly, I would just define event handlers on each group of buttons:

$('#box1 .button').on('click', function(event) {
  // ...
});

$('#box2 .button').on('click', function(event) {
  // ...
});

Comments

1

You don't need the if statement if there is no else.

$('#box1, #box2').find('.button').click(function() {
  //  do something
});

Comments

1

use $(this).parents("#box1"); inside the click event function for the buttons

Comments

0
$('#box1 .button').click(function() {
  // first code
});

$('#box2 .button').click(function() {
  // second code
});

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.