Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Closest span

Tags:

jquery

Here is my HTML code

                <span class="xxt" style="">Hide Me</span>
                <div><br/></div>
                <div></div>
                <span style="">
                    <table>
                        <tr>
                            <td style="">
                                <div></div>
                                <input type="radio" class="a">
                            </td>
                        </tr>
                    </table>
                </span>

                <span class="xxt" style="">Hide Me</span>
                <div><br/></div>
                <div></div>
                <span style="">
                    <table>
                        <tr>
                            <td style="">
                                <div></div>
                                <input type="radio" class="a">
                            </td>
                        </tr>
                    </table>
                </span>

I am trying to access the closest span element with class "xxt" which is nearer to the parent span of the changed radio button.
Here is my code

       $('.a').live('change',function()
         {
            $(this).parents('span').closest('.xxt').addClass('DestQID');
            $('.DestQID').hide();
         });

Here is my test jsfiddle

like image 310
Reddy Avatar asked Dec 12 '22 10:12

Reddy


1 Answers

Try this:

$('.a').live('change',function()
{
    $(this).closest('span').prevAll('.xxt:first').addClass('DestQID');
    $('.DestQID').hide();
});
like image 198
Igor Dymov Avatar answered Jan 24 '23 23:01

Igor Dymov