JavaScript
--
Questions
--
Followers
Top Experts
i have a nested unorderd list i.e parent-child. I have styling on the parent that is activated when hovered on with a mouse. This works fine.
However, I want this styling to remain when a child list item is hovered on too.
Is there anyway of traversing backwards so that a child list item knows its parent and therefore puts the styling on the parent?
Thanks
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
var elem=getElementById('someI
var parentE=elem.parentNode;
parentE.style.width=elem.s
but otherwise i don't think there is css support for tracking down and element's parent.
there are :child pseudo selectors .. with limited browser support.. but i dont think w3c even has a draft spec for parent selectors.
im thinking javascript would be required to traverse the DOM and find the parent and then apply styles to it.
Yes, i tried using this function:
function parentstyle(elem) {
      var parentE=elem.parentNode;
      var parentEE=parentE.parentNod
      parentEE.className="parent
  }
which is called on the child list item with this:
onMouseOver="parentstyle(t
This works fine in Firefox, but doesnt work in IE or Opera.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
It works for me in IE6 and O9... (and of course FF)
Could you post (the relevant part of) your HTML?
Yes , html below and css for the anchor tag background image i want to maintain:
<div id="primary_nav">
        <ul>
        <li><a href="index.html">Home</a>
            <li><a href="">products</a></li>
            <li><a href="">partners</a></li>
            <li><a href="">explore technologies</a></li>
            <li><a href="">Applications</a></
            <li><a href="">Purchase</a></li>
            <li><a href="">support & services</a>
            <ul>
             <li onMouseOver="parentstyle(t
             <li onMouseOver="parentstyle(t
             <li onMouseOver="parentstyle(t
             <li onMouseOver="parentstyle(t
             <li onMouseOver="parentstyle(t
             <li onMouseOver="parentstyle(t
             <li class="last"><span></span>
            </ul>
            </li>
            <li><a href="">news & events</a></li>
            <li><a href="">about us</a></li>
            <li><a href="">Contact Us</a></li>
            <li ><a href="">Training</a></li>
        </ul>
       </div>
div#primary_nav ul li a:hover, div#primary_nav ul li.parent a {
  background:url('images/pri
  color:#fff
}
div#primary_nav ul li ul li a:hover, div#primary_nav ul li.parent ul li a {
 background-image:none
}
give the <ul> an id name e.g. <ul id="unOrderedList">
and in the style sheet do the following:
#unOrderedList >Â li {
...
put you styling in here
...
}
This should make all <li> under the <ul id="unOrderedList"> have the same css styling.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Yes, but i only need to put the styling on when a list item is hovered over. If i did this wouldnt all list items have the same styling?
I don't think the problem is the parentstyle() function...
Your right, it does work actually - thanks
However, i also have a function for IE that enables list items to have a class associated to them when they are hovered over(see below). This seems to stop the other function working - perhaps because two classes are being assigned. Is there a way around this?
sfHover = function() {
      var sfEls = document.getElementById("p
      for (var i=0; i<sfEls.length; i++) {
           sfEls[i].onmouseover=funct
                 this.className+=" sfhover";
           }
           sfEls[i].onmouseout=functi
                 this.className=this.classN
           }
      }
  }
      if (window.attachEvent) window.attachEvent("onload






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
>No, but because a function
Here I meant "no, not this is the reason" and not "no there's no way around this" Â :)
sfHover = function() {
   var sfEls = document.getElementById("p
   for (var i=0; i<sfEls.length; i++) {
     Â
      sfEls[i].onmouseover=funct
         if (this.name=='special') parentstyle(this); Â
          this.className+=" sfhover";
      }
     Â
      sfEls[i].onmouseout=functi
         if (this.name=='special') parentstyleout(this);  //  <-------------------------
          this.className=this.classN
      }
    }
  }
  Â
if (window.attachEvent) window.attachEvent("onload

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
this doesnt seem to work in opera.
its fine in IE and firefox however.
Any ideas?
thanks a lot
if (this.title=='special') parentstyle(this);
and
 <li title="special" onMouseOver="parentstyle(t
This should work with Opera too.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
JavaScript
--
Questions
--
Followers
Top Experts
JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.