3

A rather simple question that I feel has a simple solution to, but can't seem to think of the right way of going about solving.

I have a set of buttons laid out in a 3 x 3 format, that I want to change attributes of when clicked, but independently. I gave them all the same class, but differentiated them using id's.

Is there a way to write, in one line for a group of ID's to do the same thing? Or do I have to redundantly write out each button's call.

I guess to put it simply, can I write

 $('.ready').click(function(){
   if('.ready'.is('#7') || '.ready'.is('#8') || '.ready'.is('#9')){
      execute code.
   }
 });

Any help would be greatly appreciated!

3
  • You can give elements more than one class too. Commented Nov 13, 2015 at 19:30
  • Use $(this) inside the function. It should work. Commented Nov 13, 2015 at 19:30
  • if ($(this).is("#7, #8, #9")) Commented Nov 13, 2015 at 19:30

3 Answers 3

1

You don't need the class .ready selector since ids are unique enough to target as selectors.

 $('#7,#8,#9').click(function()
{
    // do stuff
});
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I didn't think it was that simple! I can just do that + $(this) to get the effect I needed for each button. Thanks!
1

You can simply do

var pair1 = "#7, #8, #9";
var pair2 = "#2, #3, #4";
$('.ready').click(function(){

   if($(this).is(pair1)){
      execute code.
   }

   else if($(this).is(pair2)){
      execute some other code.
   }

});

1 Comment

Tried this block of code out as well! Works like a charm! Thank you :)
1

Use the .attr() function to get the id attribute.

$('.ready').click(function() {
    var id = $(this).attr('id');
    if (id === '7' || id === '8' || id ==='9') {
        //execute code
    }
});

Since your id's are numbers, you could also just use comparison operators. See the working example below, which will tell you which row you clicked based on the id.

$(document).ready(function() {
  $('.ready').click(function() {
    var id = $(this).attr('id');
    if (id < 4) {
      $('#out').html('First row clicked');
    } else if (id < 7) {
      $('#out').html('Middle row clicked');
    } else {
      $('#out').html('Last row clicked');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="1" class="ready">1</button>
<button id="2" class="ready">2</button>
<button id="3" class="ready">3</button>
<br>
<button id="4" class="ready">4</button>
<button id="5" class="ready">5</button>
<button id="6" class="ready">6</button>
<br>
<button id="7" class="ready">7</button>
<button id="8" class="ready">8</button>
<button id="9" class="ready">9</button>

<span id="out"></span>

1 Comment

Works pretty well! Thank you!

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.