Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 822
n00b0101 Avatar asked Dec 16 '09 04:12

n00b0101


People also ask

How to get parent value in jQuery?

The parents() method in jQuery is used to get all ancestor elements of the given selector. It is an inbuilt function in jQuery. This method traverses upwards from the parent element, all the level up in the DOM tree and returns all ancestors of the selected element.

How to target parent element using jQuery?

The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.

How to get parent element from child in jQuery?

jQuery parent() method is used to get the direct parent element of the selected HTML element. You can perform desired actions on the parent element once it Is returned. This is the syntax for using jQuery parent(): $("child").

What is the use of parent () and child () method in jQuery?

It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.


1 Answers

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>
like image 109
leepowers Avatar answered Oct 22 '22 00:10

leepowers