2

I am new to Jquery so please pardon me if I am asking a simple question but I need some help.

In my HTML markup, I have 3 divisions with id from test1 to test3 and a button.

<input type="button" id="button_click" value="ok" />
<div class="supercolor" style="width:100px;">
  <div class="colordiv" id="test1" >&nbsp;</div>
  <div class="colordiv" id="test2" >&nbsp;</div>
  <div class="colordiv" id="test3">&nbsp;</div>
</div>

What I want to do is on button click, I want to iterate through all divs in the 'supercolor' class to change the div's css background to red.

Code I have now

$('#button_click').click(function(){
   $('div.supercolor').each(function() {
   $(this).css("background-color","red");
   });
});

3 Answers 3

1
$('#button_click').click(function() {
    $('.supercolor > div').css('background-color', '#f00');
});
Sign up to request clarification or add additional context in comments.

Comments

1
$('div.supercolor div').css('background-color', 'red');

Is probably what you need.

1 Comment

I read the question as meaning all divs with the supercolor class.
1

actually if they're going to be the same color then you can just do as what minitech suggested. but just for completeness sake.

you just missed out the div in your .each.

$('#button_click').click(function(){
   $('div.supercolor > div').each(function() {
   $(this).css("background-color","red");
   });
});

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.