Search code examples
jqueryattributesparent

jquery - how to set parent attribute?


I'm trying to write an if statement where if one of the elements has it's display set to "none", I want the parent element to also display "none"...

This is the code I'm trying, which doesn't work...

/* tried this first */
if($('#prevx a').attr('display') == 'none') {
    $(this).parent().attr('display','none');
}

/* and then this */
if($('#prevxa > a').attr('display') == 'none') {
    $('#prevxa').attr('display','none');
}

The markup looks like this:

<ul>
   <li class="navnext" id="nextxa">
      <a id="nextx" href="#"><img src="/images/next.png"/></a>
   </li>

   <li class="navprev" id="prevxa">
      <a id="prevx" href="#" style="display: none;"><img src="/images/previous.png"/></a>
   </li>
</ul>

Solution

  • Try this:

    if($('#prevx').css('display') == 'none') {
        $('#prevx').parent().css('display','none');
    }
    

    Better yet:

    $('#prevx').parent().css('display',$('#prevx').css('display'));
    

    This example works for me. To hide/display the parent, toggle the child's display between none and inline:

    <ul>
       <li class="navnext" id="nextxa">
          <a id="nextx" href="#"><img src="/images/next.png"/></a>
       </li>
    
       <li class="navprev" id="prevxa">
          <a id="prevx" href="#" style="display: inline;"><img src="/images/previous.png"/></a>
       </li>
    </ul>
    
    <script>
    if ($('#prevx').css('display') == 'none') 
        $('#prevx').parent().css('display', 'none');
    else
        $('#prevx').parent().css('display', 'list-item');
    </script>