473,320 Members | 2,020 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,320 software developers and data experts.

nextSibling doesn't work

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 just
entered
if (!(e.keyCode == 16 || e.keyCode == 9))
{
if(i.value.length == 4 )
{
if(i.nextSibling != null)
i.nextSibling.focus();
}
}
}

And here's the call
<input name="text1" size="4" type="text" maxlength="4"
onkeyup="CheckFieldLength(event, this);" />

After entering 4 chars, I get a Javascript error that says
object doesn't support this property or method.

Where am I going wrong? Thanks

Jul 23 '05 #1
12 5136
"BillKi" <bi*************@yahoo.com> writes:
I am trying to use nextSibling to move focus to the next input (text
box). .... if(i.nextSibling != null)
i.nextSibling.focus(); .... After entering 4 chars, I get a Javascript error that says
object doesn't support this property or method.


At what line? My bet is that it's the "focus" property that
is missing. How do you know that the next sibling is an input
element, and not, e.g., a text node? (which it probably is :)

So, try something like this instead:
---
do {
i = i.nextSibling;
} while (i != null && i.nodeName != "INPUT");
if (i != null) {
i.focus();
}
---

/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 #2
The error occurs on the line where I try to determine if the
nextSibling is null
if(i.nextSibling != null)

Let me try your suggestion and see if that helps...

Thank you

Jul 23 '05 #3
Whoops -- your example put me into an endless loop. Had to kill the
browser to get out of it!!

Jul 23 '05 #4
BillKi wrote:
Whoops -- your example put me into an endless loop. Had to kill the
browser to get out of it!!


Please quote what you are replying to. If you are using Google groups,
click the 'Show options' link and then the 'Reply' link below subject.
Here's a variation on Lasse's loop:

while ( i.nextSibling && ( 'INPUT' != i.nodeName ) ) {
i = i.nextSibling;
}
if ( i.focus ) i.focus();
--
RobG
Jul 23 '05 #5
"BillKi" <bi*************@yahoo.com> writes:

Please quote what you are responding to. The example was:
---
do {
i = i.nextSibling;
} while (i != null && i.nodeName != "INPUT");
if (i != null) {
i.focus();
}
---
Whoops -- your example put me into an endless loop. Had to kill the
browser to get out of it!!


Interesting ...

For that code to loop endlessly, it would mean that i never becomes
equal to "null". Since "undefined" is equal to "null", you must have
an infinite (most likely cyclic) chain of non-null nextSibling
elements.

Can we see the page that exhibits this behavior?

/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 #6
RobG <rg***@iinet.net.auau> writes:
Here's a variation on Lasse's loop:
Almost :)
while ( i.nextSibling && ( 'INPUT' != i.nodeName ) ) {
If i.nodeName == 'INPUT', then the loop body is not executed, and i is
not changed. The next time through, i is still the same (input)
element. You need an extra i=i.nextSibling before the while loop.
i = i.nextSibling;
}
if ( i.focus ) i.focus();


/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 #7
Here's the whole page: The error occurs in the CheckFieldLength
function whereever I refer to i.nextSibling. The error message is
"object doesn't support this property or method"

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Testing Sibling Script</title></head>
<script type="text/javascript">
<!--
function CheckFieldLength(e, i)
{
// don't move automatically forward if a tab or shift-tab was just
entered
if (!(e.keyCode == 16 || e.keyCode == 9))
{
if(i.value.length == 4 )
{
if(i.nextSibling != null)
i.nextSibling.focus();
}
}
}

function CheckForEnter(e, f)
{
var keycode;
if (e.keyCode)
keycode = e.keyCode;
else
keycode = e.which;

if (keycode == 13)
{
f.submit();
}
}

// -->
</script>
<body>
<h1>Testing Sibling Script</h1>
<form action="testsiblings.htm" method="post" name="MainForm">
Voucher Number: <input name="text1" size="4" type="text" maxlength="4"
onkeyup="CheckFieldLength(event, this);" />
<input name="text2" size="4" type="text" maxlength="4"
onkeyup="CheckFieldLength(event, this);" />
<input name="text3" size="4" type="text" maxlength="4"
onkeyup="CheckFieldLength(event, this);" />
<input name="text4" size="4" type="text" maxlength="4"
onkeypress="CheckForEnter(event, this.form);" />
<br /><input name="btnSubmit" type="button" value="Submit" />
</form>
</body>
</html>

Jul 23 '05 #8
"BillKi" <bi*************@yahoo.com> writes:
Here's the whole page: The error occurs in the CheckFieldLength
function whereever I refer to i.nextSibling. The error message is
"object doesn't support this property or method"
In FireFox, I get the error messsage:
---
Error: i.nextSibling.focus is not a function
---
Also, I know that Gecko based browsers will have a text node as
next sibling for this HTML code, so that error is easily explained.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Ah, XHTML, then the nodeName is probably in lower case, not
upper case as my code suggested.
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Testing Sibling Script</title></head>
<script type="text/javascript">
The script element must be inside either the head or body element
in HTML. Just move the </head> after the script tag.
<!--
That HTML comment is not necessary.
function CheckFieldLength(e, i)
{
// don't move automatically forward if a tab or shift-tab was just
entered
(Be careful when posting code that your news client doesn't wrap lines.
It's a good idea to keep the posted code to 72 chars to avoid that ...
or use a client that doesn't automatically wrap lines)
if (!(e.keyCode == 16 || e.keyCode == 9))
{
if(i.value.length == 4 )
{
Ok so far ...
if(i.nextSibling != null)
i.nextSibling.focus();


I suggest changing these two lines to:
---
do {
i = i.nextSibling;
} while (i && !i.tagName ||
i.tagName.toLowerCase() != "input");
if (i != null) {
i.focus();
}
---
This is slightly longer than the previous example, because it
cares about the case of tag names.

It works in FireFox and IE.

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 #9
>>>>> I suggest changing these two lines to:
---
do {
i = i.nextSibling;
} while (i && !i.tagName ||
i.tagName.toLowerCase() != "input");
if (i != null) {
i.focus();
}
This is slightly longer than the previous example, because it cares
about the case of tag names.

It works in FireFox and IE. <<<<

And it works for me!!

Thank you for your help - Bill

Jul 23 '05 #10
Lasse Reichstein Nielsen wrote:
[...] it's the "focus" property that is missing. How do you know that the
next sibling is an input element, and not, e.g., a text node? (which it
probably is :)

So, try something like this instead:
---
do {
i = i.nextSibling;
} while (i != null && i.nodeName != "INPUT");
if (i != null) {
i.focus();
}
---


Not this way.
If you want to call focus(), test for *it*, not anything else:

var t;
if ((t = typeof i.focus) == "function" || (t == "object" && i.focus))
{
i.focus();
}

<http://pointedears.de/scripts/test/whatami>, §2.
PointedEars
Jul 23 '05 #11
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Lasse Reichstein Nielsen wrote: ....
if (i != null) {
i.focus();
}


Not this way.
If you want to call focus(), test for *it*, not anything else:


It's not testing for "focus", admitted, but it isn't testing for
anything else either, except the existence of a following input
element.

Since the "focus" method on an input element has existed in all
browsers since Netscape 2, as well as being part of the W3C DOM for
HTML since version 1, it's not something I would bother to test for.

It would be more reasonable to test for the nextSibling and tagName
properties (which the next version I posted used instead of nodeName).
var t;
if ((t = typeof i.focus) == "function" || (t == "object" && i.focus))


This would error if "i" is null, and would not otherwise make anything
safer. Remember, when we exit the previous loop, i either points to
an input element or is null.
The original poster allowed himself to assume that all the relevant
input elements of his form were siblings. I find that unlikely, and
if not, more complicated code will be needed, but this code (or rather,
the updated version posted later) solves the stated problem.

/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 #12
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:

[...]
Since the "focus" method on an input element has existed in all
browsers since Netscape 2, as well as being part of the W3C DOM for
HTML since version 1, it's not something I would bother to test for.
I read of UAs here that don't support it anyway, so disagreed. I came to
recognize that it is best to test for all host features prior to usage
rather than taking chances.
It would be more reasonable to test for the nextSibling and tagName
properties (which the next version I posted used instead of nodeName).


ACK
var t;
if ((t = typeof i.focus) == "function" || (t == "object" && i.focus))


This would error if "i" is null, and would not otherwise make anything
safer. Remember, when we exit the previous loop, i either points to
an input element or is null.


Yes, should be

var t;
if (i && ((t = typeof i.focus) == "function"
|| (t == "object" && i.focus)))

PointedEars
Jul 23 '05 #13

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

Similar topics

7
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
5
by: Gary Mayor | last post by:
Hi, If I have the ' character within the javascript:pick command it doesn't work. Is there some sort of way of escaping these characters like in server side languages. function pick(symbol) {...
3
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
4
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't...
3
by: Dave Moore | last post by:
Hi All, Ok, here's my problem. I want to open a file and process its contents. However, because it is possible that the file may not exist, I also want to check whether the file() function is...
10
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
0
by: cnb | last post by:
Python-mode worked right out out of the box in Emacs and then I added Python to my path so now the interpreter works as well. However when I try to load a file(that is not located in site-...
1
by: windscar | last post by:
Hello Everybody! I am a beginner that try to learn C++ programming. I work in Linux Ubuntu Karmic Koala (9.10) environment. I got a problem with getchar(). The function works well in a simple...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.