Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 18804
  • Last Modified:

Get Parent Element

I have a situation where I would like to find the parent element of the current element, and I'd like to do so without cluttering up my code with an ID for every element that I need to find.

For instance, say I have the following:

<div>
  <a href="javascript: SomeFunction();">Link Text</a>
<div>

I want the function that is called to find the parent <div> element without giving the parent div and ID and without needing to give the <a> element an ID

I've tried passing "this" to the function, but that gets me no where.

What would I pass to the function and then how would I locate the parent div with that information.

If this isn't clear enough, please let me know an I will supply more information if I can.
0
Hube02
Asked:
Hube02
  • 3
  • 3
2 Solutions
 
HonorGodCommented:
function SomeFunction( obj ) {
  var parent = obj.parentNode;
  ...

}

<a href="javascript: SomeFunction(this);">Link Text</a>
0
 
Hube02Author Commented:
This doesn't seem to work for links:

      function SomeFunction( obj ) {
        alert(obj);
        var parent = obj.parentNode;
        alert(parent);
      }

    <a href="javascript: SomeFunction(this);">Link Text</a>

The first alert shows "[object window]" the second "undefined"

But I'd already tested using (this) as I said.

It works if I change it to

      <span onclick="SomeFunction(this)">Link Text</span>

If this can't be done with a link <a href... then I can go to a different element type, but I was hoping to use a link to do this.

0
 
David S.Commented:
Yeah, you can't use the "this" keyword to refer to a link when you use the href="javascript:SomeFunction()"

You don't have to not use a link though.

<a href="enable-js.html" onclick="SomeFunction(this)">Link Text</a>

Do you really want to use inline event handlers?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
HonorGodCommented:
Interesting.  I hadn't realized, or I forgot, that in this situation, the "this"
passed in that kind of call would be to the document.

One option would be to have a function executed when the page loads,
that looks for these kinds of links, and changes the onclick attribute to
provide the appropriate reference.

Does this sound like a viable option to you?

0
 
Hube02Author Commented:
I think I'm going to end up going with using a span and using CSS to make it look like a link. The reason that I wanted to avoid this is the fact that IE6 does not support the pseudo of hover on anything but a link and I don't really want to implement the htchover gizmo for just this.
0
 
HonorGodCommented:
Well, thank you for sharing your decision with us.  This is one of the
reasons that I find experts-exchange so useful.

Thanks for the grade & points.

Good luck & have a great day
0
 
Hube02Author Commented:
I know that sometimes the answer is "No, that can't be done that way." And for me this is an acceptable answer. I find these answers just as useful as the ones that tell me how to do something, at least I know I need to head in another direction.

I appreciate the fact that I can just discuss a question with others rather than smacking my head on the monitor.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now