473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

trouble using .nextSibling

During the window.onload event, I set the .onclick event of an element
to turn off the display of the first two elements in a <div> called
"content":

var objNode = getElementById("content").getElementsByTagName("h1 ")[0];
objNode.style.display = "none";
objNode.nextSibling.style.display = "none";

Unfortunately, this (and several other slight variations on this) was
unable to recognize "nextSibling" as a function. What is it I'm not
understanding here?

(On a related note, I probably would have used the .firstChild()
function to grab that header, but I had no luck with that either, and
..nextSibling is the more serious problem.)

Jul 23 '05 #1
5 6268


2obvious wrote:
During the window.onload event, I set the .onclick event of an element
to turn off the display of the first two elements in a <div> called
"content":

var objNode = getElementById("content").getElementsByTagName("h1 ")[0];
objNode.style.display = "none";
objNode.nextSibling.style.display = "none";


(Ignoring that it should be document.getElementById and not
getElementById) I do not see where you turn off the display "of the
first two elements in a <div called 'content'", you are trying to access
the first <h1> element and its next sibling. Several things can fail, it
is possible there is no <h1> element, then you already get an error with
objNode.style. Or the next sibling node is not an element node but a
text node or comment node for instance which does not have a style
property. Or there is no next sibling meaning nextSibling gives null. If
you want to write code that works generally without throwing errors then
you need to use checks like
if (objNode && objNode.style) {
objNode.style.display = 'none';
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
"2obvious" <Ev************@gmail.com> writes:
var objNode = getElementById("content").getElementsByTagName("h1 ")[0];
I assume that objNode is not null (i.e., the page contains what you
expect).
objNode.style.display = "none";
Try adding
alert(objNode.nextSibling.nodeType);
(explanation later)
objNode.nextSibling.style.display = "none";

Unfortunately, this (and several other slight variations on this) was
unable to recognize "nextSibling" as a function.
Good, because "nextSibling" is not a function, but a property (as you
are using it).
What is it I'm not understanding here?

(On a related note, I probably would have used the .firstChild()
function to grab that header, but I had no luck with that either, and
.nextSibling is the more serious problem.)


I will wager a guess: You are using a Gecko based browser (e.g.,
Mozilla or Firefox).

If you add the alert from above, it will tell you what kind of DOM
node the nextSibling node is. To be an element node (and have a style
property), the value of nodeType must be 3. I'm guessing that it will
alert "7", the type of a text node. It is the text node that contains
the whitespace (probably a newline and some spaces) between the "h1"
element and the following element.

You can't just use .nextSibling.nextSibling to step over the text node,
because IE, in it's infinite wisdom, does not include text nodes that
only contain whitespace in its document model. You need to find the
next element. You can use a function like this;
---
function nextSiblingElement(elem) {
do {
elem = elem.nextSibling;
} while (elem && elem.nodeType != 3);
return elem;
}
---

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
On Thu, 14 Jul 2005 14:52:46 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
You can't just use .nextSibling.nextSibling to step over the text node,
because IE, in it's infinite wisdom, does not include text nodes that
only contain whitespace in its document model.


Which isn't actually non-standard behaviour for even an XML DOM it's
as if a normalizeDocument() has been called with an appropriate
DOMConfiguration. Applications are free to do this.

Jim.
Jul 23 '05 #4
Thanks for all the input, guys.

My previous code was obviously not lifted from the original source;
here is the actual code:

var topNode =
document.getElementById("content").getElementsByTa gName("h1")[0];

if ( topNode.style.display != "none" )
{
topNode.style.display = "none";
topNode =
document.getElementById("content").getElementsByTa gName("p")[0];
topNode.style.display = "none";
}

I use IE6 for most tasks, but I test under six different
browsers/versions. Yes, I use a Gecko browser for javascript
testing--Netscape 7. Explorer isn't too helpful when it comes to
debugging errors.

The code in content looks like this:

<div id="content">
<h1></h1>
<p></p>
<div>
...
</div>
</div>

When I use this line:

alert(topNode.nextSibling.node*Type);

The response I get is "undefined."

My understanding of the node object is clearly lacking, which is the
crux of my question. White space gets considered when working with
nodes? That's total news to me. I think of nodes the way I think of
elements. Apparently I'm wrong?

Jul 23 '05 #5
On a related note: I could easily solve this problem if I wrapped the
top few elements in a <div>. I haven't done that because I want as
little nonstructural HTML as possible. Do you think I've made a wise
choice, or is easy way out a better way out?

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paweł Gałecki | last post by:
I wrote a following function just for testing purposes: <html> <head> <script> function show(obj) { alert(obj.previousSibling.rowIndex) alert(obj.rowIndex) alert(obj.nextSibling.rowIndex) }...
12
by: BillKi | last post by:
I am trying to use nextSibling to move focus to the next input (text box). Here's the script: function CheckFieldLength(e, i) { // don't move automatically forward if a tab or shift-tab was...
4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
6
by: Marc | last post by:
This is probably a no brainer for the most of you but my head is spinning at the moment. Considering the following list how do I get a reference to the ul just below the li with id products?...
2
by: VK | last post by:
Is there any functional or contextual difference between elemRef.firstChild and elemRef.nextSibling ? From MDC I did not get any.
1
by: Govilakshmi | last post by:
please help me that what is meant by nextsibling?
8
by: shyamg | last post by:
plzzz help me............
1
by: =?Utf-8?B?RGF2aWRHQg==?= | last post by:
OK, so I've created and loaded an XMLDocument object. But how do I go about using it? Specifically, how do I: 1) move to the first node (I assume I start on it when I load the XML?) 2) move to...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.