1

i need help :) I have a Div, and in this Div are many other Div's with a Class. And i need to change only the last Div's Class inside the Div:

    <div id='main'>

    <div class='inside'></div>
    <div class='inside'></div>
    <div class='inside'></div>    <- Only this to be changed?

    </div>

I have a jQuery function that searches for every div inside the main Div:

    $('#main').find('.inside').each(function() {

    alert($(this).attr('class'));

    });

This does work but when i try to change only the last Divs Class it changes the Classes of all Divs inside the main Div. Is it possible to change only the last inside class Div?

Thanks!

Thorsten
  • 175
  • 1
  • 1
  • 12
  • possible duplicate of [How do I select the last element with a specified class from descendants of this?](http://stackoverflow.com/questions/8323096/how-do-i-select-the-last-element-with-a-specified-class-from-descendants-of-this) – Felix Kling Feb 05 '13 at 14:49

2 Answers2

1

You can use .last:

$('#main .inside').last().addClass(....);

The jQuery documentation is quite extensive and spending a little time to read through it is worthwhile.

Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
0

To change css use .css

$('#main .inside:last').css('width','100');
Anton
  • 31,487
  • 5
  • 43
  • 54