There is such a piece of layout:

<div class="1"> <a href="#">Some link</a> <span><input class="selected/unselected"></span> </div> 

I wanted to ask how you can choose a div class 1 if he has an insideted select inside (that is, if inside a div is inside an selected, then the div should also become selected), and remove the class if the input becomes unselected. Inputs change their class if you click on another button on the site.

    4 answers 4

     $('div.1').each(function(){ if($.contains(this, $('input.selected', this)[0])) $(this).addClass('selected'); }); $('div.1 input').live('focusin focusout', function(){ $(this).toggleClass('selected unselected').closest('div.1').toggleClass('selected unselected'); }); 
    • I really liked your decision, thank you very much - makregistr

    If the code is:

     <div class="1 unselected"> <a href="#">Some link</a> <span><input class="selectable unselected"></span> </div> 

    Then on jQuery there will be something like this handler:

     $('div.1>span>input.selectable').live('focus blur', function(event) { $(event.target).toggleClass('selected unselected'); var $parent = $(event.target) .parent() .parent(); if($parent.hasClass('1')) { $parent.toggleClass('selected unselected'); } }); 

    you can look here

      And why it is impossible to put a class immediately on a div, and not to put on input at all? Now you want, instead of solving a single problem, to create a new one.

      • you can't hang on the div at once, there is some functionality on the site, long to tell - makregistr
      • Well now not some will be :) - Artyom Sapegin
       $('div.1 input').each(function(){ $(this).closest('div.1').addClass( $(this).hasClass('selected')?'selected':'' ); $(this).closest('div.1').removeClass( $(this).hasClass('selected')?'':'selected' ); });