473,408 Members | 1,700 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,408 software developers and data experts.

Next form element

I'm trying to make it so that if the user sets focus to a form element
(text), it automatically sets focus to the very next form element. This
will, in effect, prevent the user from modifying the value. I know I can do
this by setting the form element to 'readonly' but that's not possible as
I'll need to modify that value elsewhere in my JS. Anyway...

I know I can do something like this:

document.forms[1].elements[4].focus().

Only problem for me with the above method is that I'm generating my form
dynamically so I won't know a) which element number the current field is and
b) which, if any, is the next element. I've tried setting up an event
handler so:

OnFocus='this.nextSibling.focus();'

but that doesn't work. Apparently, I can't use nextSibling when working
with a form element. So I'm wondering how I can do what I'm needing to do?

Any help would be greatly appreciated!

thnx,
Chris
Jan 30 '06 #1
15 2790
Christoph wrote on 30 jan 2006 in comp.lang.javascript:
I know I can do
this by setting the form element to 'readonly' but that's not possible
as I'll need to modify that value elsewhere in my JS.


1 you can write the value of a readonly <input> element with javascript

2 you cannot change the readonlyness of an <input> element with javascript

3 however, you can use "disabled" to your advantage.

<input id=x disabled>

<script type="text/javascript">
var x = document.getElementById('x')
x.value = 'hi world'
alert(x.disabled)
x.disabled = false
</script>

Do not mess with focus() to restrain the user!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 30 '06 #2
Christoph wrote:
[...]
Only problem for me with the above method is that I'm generating my form
dynamically so I won't know a) which element number the current field is
and
b) which, if any, is the next element. I've tried setting up an event
handler so:

OnFocus='this.nextSibling.focus();'

but that doesn't work.
It does not work because `this.nextSibling' does not refer to an Element
object that represents a form control but probably a TextNode object that
has no such method.
Apparently, I can't use nextSibling when working with a form element.
You can.
So I'm wondering how I can do what I'm needing to do?


Iterate until you encounter a node that represents a form control.
Quickhack:

function isMethodType(s)
{
return (s == "function" || s == "object");
}

function getNextControl(o)
{
if (o)
{
while ((o = o.nextSibling))
{
if (isMethodType(typeof o.focus)
&& /input|button|select/.test(o.tagName.toLowerCase()))
{
return o;
}
}
}

return {focus: function() {}};
}

<... onfocus="getNextControl(this).focus();"
PointedEars
Jan 30 '06 #3
> "Evertjan." <ex**************@interxnl.net> wrote:
news:Xn********************@194.109.133.242....

Christoph wrote on 30 jan 2006 in comp.lang.javascript:
I know I can do
this by setting the form element to 'readonly' but that's not
possible as I'll need to modify that value elsewhere in my JS.
1 you can write the value of a readonly <input> element with
javascript

2 you cannot change the readonlyness of an <input> element with
javascript


I thought one could toggle readOnly between true and false.

What would the situation be that this does not work in?
3 however, you can use "disabled" to your advantage.

<input id=x disabled>
<button type="button" onclick="x.readOnly=(x.readOnly)?false:true;
x.focus(); x.select();">toggle readOnly</button>
<script type="text/javascript">
var x = document.getElementById('x')
x.value = 'hi world'
alert(x.disabled)
x.disabled = false
</script>

Do not mess with focus() to restrain the user!


--
BootNic Tuesday, January 31, 2006 12:53 PM

Inform all the troops that communications have completely broken down.
*Ashleigh Brilliant*

Jan 31 '06 #4
BootNic wrote on 31 jan 2006 in comp.lang.javascript:
<button type="button" onclick="x.readOnly=(x.readOnly)?false:true;
x.focus(); x.select();">toggle readOnly</button>


A <button> is always a button.

onclick="x.readOnly=!x.readOnly;"

try:

==============
<input id='x' readonly>

<script type="text/javascript">
var x = document.getElementById('x')
</script>

<button type="button"
onclick="x.readOnly=!x.readOnly;">
toggle readOnly</button>
===============

Indeed it works!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 31 '06 #5
BootNic wrote:
"Evertjan." [...] wrote: [...]
Christoph wrote on 30 jan 2006 in comp.lang.javascript:
I know I can do this by setting the form element to 'readonly' but
that's not possible as I'll need to modify that value elsewhere in
my JS.
1 you can write the value of a readonly <input> element with
javascript

2 you cannot change the readonlyness of an <input> element with
javascript


I thought one could toggle readOnly between true and false.


You can, but as was said correctly, you do not have to in order to change
the value of the respective control.
What would the situation be that this does not work in?


An unsupportive DOM, particulary a DOM that does not implement the
HTMLInputElement interface of the W3C DOM. However, such DOMs are rare
nowadays: this property is supported r/w for those element objects by the
the Gecko DOM, the IE4+ DOM, the Opera 6+ DOM and the KHTML 2.2+ DOM. That
covers practically almost all known HTML user agents, including Mozilla,
Mozilla Firefox, Internet Explorer 4+, Opera 6+, Konqueror 2.2+ and Safari.
It does not cover Netscape 4.x, for example.
3 however, you can use "disabled" to your advantage.

<input id=x disabled>


<button type="button" onclick="x.readOnly=(x.readOnly)?false:true;
x.focus(); x.select();">toggle readOnly</button>


0. The button will be useless to users without client-side script support.

1. Referring to an element object by the ID of the element it represents is
a deprecated and often IE-only practice. If the `button' element is
descendant of the same `form' element as the `input' element, it is
better to use this.form.elements['x'] instead.

2. readOnly is a boolean property already.

3. The `button' element is not backwards compatible and it is unnecessary
here (as the element content is a text node).

<script type="text/javascript">
document.write(
'<input type="button" value="toggle readOnly"'
+ 'onclick="var x = this.form.elements['x'];'
+ ' x.readOnly = !x.readOnly; x.focus(); x.select();">');
</script>
<script type="text/javascript">
var x = document.getElementById('x')
x.value = 'hi world'
alert(x.disabled)
x.disabled = false
</script>

Do not mess with focus() to restrain the user!


You are not referring to this, so do not quote it.
PointedEars
Jan 31 '06 #6
JRS: In article <Xu*****************@newssvr24.news.prodigy.net> , dated
Tue, 31 Jan 2006 17:53:59 remote, seen in news:comp.lang.javascript,
BootNic <Bo*****@bounce.prodigy.net> posted :

<button type="button" onclick="x.readOnly=(x.readOnly)?false:true; ^^^^^^^^^^^^ Ugh!x.focus(); x.select();">toggle readOnly</button>

As is well-known, I dislike using new-fangled constructs instead of
older equivalents as soon as they become available in the latest
systems, since that handicaps those with older software.

And I don't recall ever seeing <button ... > in anyone else's source.

But (with or without type=button) it works in my IE4, and is acceptable
to two local testers.

In which versions, at about what date, did it appear in browsers?

Are there other special cases of <input type=... > that are widely
implemented?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 1 '06 #7
Dr John Stockton said the following on 2/1/2006 11:16 AM:
JRS: In article <Xu*****************@newssvr24.news.prodigy.net> , dated
Tue, 31 Jan 2006 17:53:59 remote, seen in news:comp.lang.javascript,
BootNic <Bo*****@bounce.prodigy.net> posted :
<button type="button" onclick="x.readOnly=(x.readOnly)?false:true; ^^^^^^^^^^^^ Ugh!
x.focus(); x.select();">toggle readOnly</button>

As is well-known, I dislike using new-fangled constructs instead of
older equivalents as soon as they become available in the latest
systems, since that handicaps those with older software.


<button> is not a "new-fangled" construct though. It is at least 7 years
old and probably older.
And I don't recall ever seeing <button ... > in anyone else's source.
It is seen quite a lot.
But (with or without type=button) it works in my IE4, and is acceptable
to two local testers.

In which versions, at about what date, did it appear in browsers?
It is supported by IE4+, NS6+ and any later (modern) browser. NN4 didn't
support button though.
Are there other special cases of <input type=... > that are widely
implemented?


Such as what?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 1 '06 #8
> "Evertjan." <ex**************@interxnl.net> wrote:
news:Xn********************@194.109.133.242....

BootNic wrote on 31 jan 2006 in comp.lang.javascript:
<button type="button" onclick="x.readOnly=(x.readOnly)?false:true;
x.focus(); x.select();">toggle readOnly</button>


A <button> is always a button.


If I were to put a button in a form without type="button", and it was
not the submit button, some browsers treat it as a submit button. I
usually add it for that reason.

IE 6 does not seem to have any issues with it.

FireFox 0.8.0
NetScape 7.2
Mozilla 1.7.12
Opera 8.51

All treat it as a submit button.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<form id="a" name="a" action="http://www.google.com/search">
<input name="q" type="text" value="button collector"><br>
<button type="button" onclick="alert('hello')">Alert
Hello</button><br>
<button onclick="alert('Good Bye')">Alert Good Bye</button><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

--
BootNic Wednesday, February 01, 2006 10:12 PM

When I was young, I was put in a school for retarded kids for two
years before they realized I actually had a hearing loss...and they
called ME slow!
*Kathy Buckley*


Feb 2 '06 #9
BootNic wrote:
"Evertjan." [...] wrote:
BootNic wrote on 31 jan 2006 in comp.lang.javascript:
<button type="button" onclick="x.readOnly=(x.readOnly)?false:true;
x.focus(); x.select();">toggle readOnly</button> A <button> is always a button.


If I were to put a button in a form without type="button",
and it was not the submit button, some browsers


All widely distributed browsers except IEeek do.
treat it as a submit button.
Because it is specified so:

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-BUTTON>

(The third value of an attribute declaration is the default value.)
[...]
IE 6 does not seem to have any issues with it.


Because IEeek is broken. Nothing new here.
PointedEars
Feb 2 '06 #10
Thomas 'PointedEars' Lahn wrote on 02 feb 2006 in comp.lang.javascript:
If I were to put a button in a form without type="button",
and it was not the submit button, some browsers


All widely distributed browsers except IEeek do.


I did not know hat.

But then, I personally always use <button> OUTSIDE a <form>,
and <input type= submit/reset INSIDE.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 2 '06 #11
JRS: In article <-P********************@comcast.com>, dated Wed, 1 Feb
2006 16:00:06 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Are there other special cases of <input type=... > that are widely
implemented?


Such as what?


If I knew what, I would not need to ask.

To explain to you in more detail :

In HTML, within <input > one can put type="..." and get an
input control of the type indicated by what is where I have put three
dots. It now seems to be established that <input type=button and
<input type="button" can be replaced by the simpler <button .

Examples : type=text type=radio type=password type=checkbox

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 2 '06 #12
Dr John Stockton wrote on 02 feb 2006 in comp.lang.javascript:
It now seems to be established that <input type=button and
<input type="button" can be replaced by the simpler <button .


<button onclick='..'>Press</button>

equals

<input type='button' onclick='..' value='Press'>

.... well sort of, I hope.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 2 '06 #13
"Evertjan." <ex**************@interxnl.net> writes:
<button onclick='..'>Press</button>

equals

<input type='button' onclick='..' value='Press'>

... well sort of, I hope.


Actually it equals
<input type="submit" onclick="..">Press</button>
except that the rendering might be a little different too.

The button element has a type attribute taking one of the three
values: "button", "submit" and "reset", with "submit" being the
default. They work the same as the input elements with the same
type attribute.

The content of a button element can be arbitrary inline HTML. That
means that it might have a text baseline different from the
surrounding text (in some browsers it does for sure, can't remember
which).

/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.'
Feb 2 '06 #14
Lasse Reichstein Nielsen wrote on 02 feb 2006 in comp.lang.javascript:
The content of a button element can be arbitrary inline HTML.
Which I think a definite improvement over <input button=''..:

<button>
Please press <span style='color:red;'>here</span> now!
</button>
That
means that it might have a text baseline different from the
surrounding text (in some browsers it does for sure, can't remember
which).


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 2 '06 #15
Dr John Stockton said the following on 2/2/2006 7:10 AM:
JRS: In article <-P********************@comcast.com>, dated Wed, 1 Feb
2006 16:00:06 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Are there other special cases of <input type=... > that are widely
implemented? Such as what?


If I knew what, I would not need to ask.


Fair enough.

To explain to you in more detail :

In HTML, within <input > one can put type="..." and get an
input control of the type indicated by what is where I have put three
dots. It now seems to be established that <input type=button and
<input type="button" can be replaced by the simpler <button .

Examples : type=text type=radio type=password type=checkbox


No, only <button> has managed to mutate from <input type="button">.

One thing I have never understood, and probably never will, is why
<button> defaults to a submit button. <button type="button"> Seems
overkill. Leave it a button by default, same as input is text by
default. If you want something else then you give it the type.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 3 '06 #16

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

Similar topics

12
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
7
by: mcanedo | last post by:
Hi: I have several text elements in a form. Some of they are filled by the user, and then some are automatically filled based on the user input. Therefore I do not want the user to be able to...
1
by: john woo | last post by:
Hi I'm not good at JS, but want to get more about it. I want to use a JSP (the java code just used to get date, the rest are html and javascript), to display a table. the requirement is the...
2
by: Deniz Bahar | last post by:
Hi, I'm working with a single linked list and want to delete elements by searching through the list (starting form the HEAD) then finding the element, then doing the following: NewElement =...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
2
by: zek2005 | last post by:
Hi!!! I've made a form with a 'select' statement through a echo('<select name="res">'); echo('<option selected value=""> Select option</option>");'); while($row = mysql_fetch_array($resumen))...
2
by: yawnmoth | last post by:
Say I have two input elements and that I wanted to make it so that when the first ones input was what it should be, the focus would automatically be shifted to the next input element. ie....
2
dlite922
by: dlite922 | last post by:
Before traversing my code, here's what my goal is and what this function does: I have a table of fields that dynamically grows as the user enters information. A minimum of 3 rows must always...
10
by: removeps-groups | last post by:
How to display table and select next to each other? <html> <body> <table border=1 style="display:inline-table"> <tr><td>Hello1</td></tr> <tr><td>Hello2</td></tr> <tr><td>Hello3</td></tr>...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.