Forum Moderators: open

Message Too Old, No Replies

Get the id attribute's value of the nextSibling node?

         

JAB Creations

11:58 am on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been reading a lot about "nodes" in the W3C DOM this morning as I've been working on creating a reusable element creation function.

I was looking for a way to insertAfter however it was all copy and paste without examples...worse none of it seemed to reference element id's.

That has led me to nextSibling. What I'm trying to do is figure out how to reference the next element's id attribute value. For example...

<div id="el1">element 1</div>
<div id="el2">element 2</div>

var parent = el1;
alert(parent + ' == ' + document.getElementById(parent).firstChild.nodeValue);

To dynamically determine the next element's id (as in not statically setting an id) I have been trying stuff like this...

alert(document.getElementById(parent).nextSibling.id);

That does not work...the desired result would alert 'el2' from the above XHTML. So all I want to do is alert the following element's id. Suggestions please?

- John

JAB Creations

1:36 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



alert(document.getElementById(parent).nextSibling.nextSibling.id);

RonPK

1:41 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's probably because the silly browser thinks a whitespace character or newline between the two elements is an element.

Dabrowski

2:24 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<div id="el1">element 1</div>  
<div id="el2">element 2</div>

var parent = el1;
alert(parent + ' == ' + document.getElementById(parent).firstChild.nodeValue);
alert(document.getElementById(parent).nextSibling.id);

Setting parent to eaqual el1 like that would refer to the object el1. That's not the right parameter to pass to getElementById, did you mean to quote it as a string?

var parent = "el1";

Not sure what you're setting parent to in your dynamic function, maybe just a simple mistake?

JAB Creations

3:13 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a really slimmed down version...people reading it in the future should know how to obviously integrate the code.

I'm going to try and create some variations in the XHTML code and try to see if it's a pattern for Gecko/Opera/WebKit versus IE's interpretation or if it's going to be too goofy to be reliable to become part of a reusable function.

- John

JAB Creations

3:26 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would this be considered a reliable method of DOM sniffing to executing code only for IE or all other browsers?

- John

if (window.ActiveXObject) {alert('is IE');}
else if (window.XMLHttpRequest) {alert('not-IE');}

Dabrowski

3:46 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you never seen this?

navigator.userAgent.indexOf( "MSIE")

JAB Creations

4:06 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's part of the user agent then it can be spoofed.

- John

Dabrowski

4:58 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So? Javascript can be easily edited. Are you trying to write a good piece of code, or a security suite?

There are also slight variations in some CSS properties if you want to check the user hasn't double-spoofed you.