Jump to content

How do I generically reference following-sibling


javajoemorgan

Recommended Posts

This is what I am trying to do:

function passNode(node) {   // do whatever}

Then, in the HTML, I'm trying something like this:

   <a href="whatever" onclick="passNode(following-sibling);">Click Me</a>   <anyOtherHtmlElement>Blah Blah Blab</anyOtherHtmlElement>

That is, I want to generically reference the node following the A within the onclick handler? All ideas are appreciated.P.S. I've already had the idea of using an ID in the following element, and then just hard-coding the ID... however, I'd really like something more generic than that... because the HTML code is generated via XSLT and uniquefying1 the IDs will be burdensome in the XSLT.

1:) Yeah... uniquefying is MY word... not George Bush's. It is the process of "uniquefication" :)
Link to comment
Share on other sites

Different browsers build the DOM differently, so, unfortunately, passing "this.nextSibling" isn't going to be compatible across browsers.Check out W3Schools XML DOM tutorial where they talk about the nextSibling property:http://www.w3schools.com/dom/prop_node_nextsibling.aspUsing that, you could do something like this:

function get_nextsibling(n){x=n.nextSibling;while (x.nodeType!=1)  {  x=x.nextSibling;  }return x;}function passNode(node) {   // do whatever}

<a href="whatever" onclick="passNode(get_nextsibling(this));">Click Me</a><anyOtherHtmlElement>Blah Blah Blab</anyOtherHtmlElement>

Link to comment
Share on other sites

Different browsers build the DOM differently, so, unfortunately, passing "this.nextSibling" isn't going to be compatible across browsers.Check out W3Schools XML DOM tutorial where they talk about the nextSibling property:http://www.w3schools.com/dom/prop_node_nextsibling.asp
Dang! Thanks was too easy a question for you guys.... I need to catch up on my reading.... (or learn better googling techniquess :) ) thanks!
Link to comment
Share on other sites

Actually, a lot of stuff is better learned by poking around. You got Firefox? Spend some quality time poking around lots of different pages with your DOM inspector. You want to see with your own eyes how stuff gets represented in the DOM.The big thing about siblings is that all those empty spaces and newlines between elements typically get turned into text nodes with the content " " or something like that. Because the browser just assumes you wanted it that way.That's why, when you go messing with children and siblings and you want something in particular, you have to test for a node type or tag name or something.And I didn't learn that by reading it. I learned it by getting frustrated, like you, and then looking under the hood. Then comes the big "Aaaaah" moment. Then you test another browser with the best tools you've got for it. (Once you know what you're looking for, creative use of alert() can tell you most of the story.)Happy hunting.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...