We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

referencing elements without ID's

D4Ly
D4Ly asked
on
Medium Priority
819 Views
Last Modified: 2012-06-27
If I have HTML like this

<ul>
        <li><a href="javascript://" title="Toggle" onclick="menu_toggle();">1</a>
            <ul id="one">
                  <li>1a</li>
                  <li>1b</li>
                  <li>1c</li>
                  <li>1d</li>
            </ul>
      </li>
      <li><a href="javascript://" title="Toggle" onclick="menu_toggle();">2</a>
            <ul id="two">
                  <li>2a</li>
                  <li>2b</li>
                  <li>2c</li>
            </ul>
      </li>
</ul>

What is the best way to do the following:
1) Pass a refrence to the menu_toggle function for the <a> element being clicked.
2) Select the parent element (the <li> that the link is nested in)
3) select the <ul> within that <li>

The addition of id's and such is not allowed!! Thanks!!
Comment
Watch Question

Commented:
Not really clear on all your questions/what you're asking for but best way to pass reference to the function is:

<a href="javascript:void(0);" title="Toggle" onclick="menu_toggle(1);">1</a>

Would help to see your toggle_menu() function but rebuild it something like:

function menu_toggle (linkID) {
if (linkID == 1) {
// do this
} else if (linkID == 2) {
//do this
}
}

If I'm missing your point altogether...please provide more details of whay you're trying to accomplish.

-Sam
Pravin AsarPrincipal Systems Engineer
CERTIFIED EXPERT
Top Expert 2005
Commented:
This one nagivates through DOM, without reference to ids

<html>
<head>
<title>GetChildByTagName Document</title>
</head>

<body>
<script language="javascript">
function getChildByTagName (obj, tagName) {
     if (!obj) { return null; }
     for (var ix=0; ix < obj.childNodes.length; ix++) {
          var cObj = obj.childNodes[ix];
          if (!cObj || !cObj.tagName) { continue; }
          if (cObj.tagName.match(tagName)) {
                  return cObj;
          }
          if (cObj.childNodes.length) {
                    cObj = getChildByTagName(cObj, tagName);
                  if (cObj && cObj.tagName.match(getChildByTagName)) { return cObj; }
          }
           
     }
}
function getParent (src, tagName) {
     while (src.parentNode != null) {
          if (src.parentNode.tagName  == tagName) {
               return src.parentNode;
          }
          src = src.parentNode;
     }
     return src;
}
function menu_toggle(aObj) {
      var pObj= getParent(aObj, 'LI');
         if (pObj) {
      var uObj =getChildByTagName (pObj, 'UL');
               if (uObj) { alert (uObj.innerHTML); }
         }
      retun (false);
}
</script>
<ul>
      <li><a href="#" title="Toggle" onclick="return menu_toggle(this);">1</a>
            <ul id="one">
                  <li>1a</li>
                  <li>1b</li>
                  <li>1c</li>
                  <li>1d</li>
            </ul>
      </li>
      <li><a href="#" title="Toggle" onclick="return menu_toggle(this);">2</a>
            <ul id="two">
                  <li>2a</li>
                  <li>2b</li>
                  <li>2c</li>
            </ul>
      </li>
</ul>

</body>
</html>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
Nearly same answer as pravinasar, simpler maybe, but taking into account that the LI is always the parent of your A node.
The reference of A node is passed through this in the function call, the LI node is reached through this.parentNode, and then a function will find the first UL child of this LI element (will return false if no childNode with this tagName).

In this example, i added the fact to show/mask the UL element on link click.

<script>
function menu_toggle(obj){
 var liNode = obj.parentNode;
 var ulNode = getFirstChildNodeByTagName(liNode,"UL");
 if (ulNode) ulNode.style.display = ((ulNode.style.display=="none") ? "" : "none");
}
function getFirstChildNodeByTagName(parent,tagName)
{
  for (var i=0;i<parent.childNodes.length;i++) {
      if (parent.childNodes[i].tagName == tagName) return parent.childNodes[i];
  }  
  return false;
}
</script>
<ul>
        <li><a href="javascript://" title="Toggle" onclick="menu_toggle(this);">1</a><ul id="one">
               <li>1a</li>
               <li>1b</li>
               <li>1c</li>
               <li>1d</li>
          </ul>
     </li>
     <li><a href="javascript://" title="Toggle" onclick="menu_toggle(this);">2</a>
          <ul id="two">
               <li>2a</li>
               <li>2b</li>
               <li>2c</li>
          </ul>
     </li>
</ul>

Author

Commented:
Smaccari-
Perfect! And the next step for me was in fact to add the display :) Thanks much, have a great day. I will give some credit to pravinasar, weighted to you for the simpler solution. Thanks!!!

- D4
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.