Need Help With My Code to Select Contents of Textbox

M

Mad Ape

I am trying to create a simple javascript - my first so go easy on me. I
want to have a script that selects the contents of a textbox when the
web page loads. I can not get what I have to work.

Please help me fix it. I think I may have the sequencing wrong but I
tried a bunch of stuff with no luck.

This code will all be put into a page with .php extension if that matters.

Thanks

The Mad Ape
www.tatumba.com

++++++++++++++++++++++++++++++++++++++++++++++++

<body onLoad = "focusIt()">

form action="" method="POST">
<input name="LOGIT" type="text" value="Some Text To Get Selected On Load
of HTML Page" size="60" />
<input type="submit" name="Submit" value="Wassup"
</form>

function focusIt()
{
var mytext = document.getElementById("LOGIT");
mytext.focus();
}
</script>

</body>

++++++++++++++++++++++++++++++++++++++++++++++++
 
M

Mad Ape

Mad said:
I am trying to create a simple javascript - my first so go easy on me. I
want to have a script that selects the contents of a textbox when the
web page loads. I can not get what I have to work.

Please help me fix it. I think I may have the sequencing wrong but I
tried a bunch of stuff with no luck.

This code will all be put into a page with .php extension if that matters.

Thanks

The Mad Ape
www.tatumba.com

++++++++++++++++++++++++++++++++++++++++++++++++

<body onLoad = "focusIt()">

form action="" method="POST">
<input name="LOGIT" type="text" value="Some Text To Get Selected On Load
of HTML Page" size="60" />
<input type="submit" name="Submit" value="Wassup"
</form>

function focusIt()
{
var mytext = document.getElementById("LOGIT");
mytext.focus();
}
</script>

</body>

++++++++++++++++++++++++++++++++++++++++++++++++

<form action="" method="POST"> is the way I have it. The missing <in
<form action is not the problem. Sorry for that.

The Mad Ape
www.tatumba.com
 
S

SAM

Le 12/5/08 7:14 PM, Mad Ape a écrit :
I am trying to create a simple javascript - my first so go easy on me. I
want to have a script that selects the contents of a textbox when the
web page loads. I can not get what I have to work.
(...)

<body onLoad = "focusIt()">

form action="" method="POST">
<input name="LOGIT" type="text" value="Some Text To Get Selected On Load
of HTML Page" size="60" />
<input type="submit" name="Submit" value="Wassup"
</form>

function focusIt()
{
var mytext = document.getElementById("LOGIT");


getElementById ... !
^^

Where do you see that your element has an id ?

an "ID" and not a "name" !

With a name, if this form is the first in your page, you can use :

var mytext = document.forms[0].elements['LOGIT'];

or (with/without name and/or id) :

var mytext = document.forms[0][0];


mytext.focus();
}
</script>



Or you keep your JS and you correct your HTML :

<input name="LOGIT" id="LOGIT" type="text" value ...
 
J

Jeremy J Starcher

Le 12/5/08 7:14 PM, Mad Ape a écrit :

Where do you see that your element has an id ?

an "ID" and not a "name" !


True, but it an easy mistake for novices to make since a lot of examples
manage to confuse the two and IE's loose handling of things does not help.

Mad Ape: My recommendation, as a general rule is:
Use an ID on something that is not part of a form. (And the form itself,
if needed.)

Use names on the elements INSIDE the form, when you can.

Don't ever use the same ID more than once. (Invalid, but a lot of UAs
let you get away with it.)

Don't use the same text for both a name -and- an id, less it is on the
same element.

For example:
<textarea name="hi" id="hi"></textarea> is valid.

But...
<textarea id="hi"></textarea>
<textarea name="hi"></textarea>

*is* valid, but is known to cause confusion with Internet Explorer-based
browsers.
 
T

Thomas 'PointedEars' Lahn

Mad said:
<body onLoad = "focusIt()">

<form action="" method="POST">
<input name="LOGIT" type="text" value="Some Text To Get Selected On Load
of HTML Page" size="60" />

You don't want to do that as it makes scrolling with the keyboard impossible.


PointedEars
 
D

dhtml

Jeremy said:
True, but it an easy mistake for novices to make since a lot of examples
manage to confuse the two and IE's loose handling of things does not help.

Mad Ape: My recommendation, as a general rule is:
Use an ID on something that is not part of a form. (And the form itself,
if needed.)

There is no good reason not to assign a form control an ID. It is often
useful, for getElementById lookup, for explicit LABEL element to work.
Use names on the elements INSIDE the form, when you can.

Where needed. Named controls (that are not disabled) can be successful.
Don't ever use the same ID more than once. (Invalid, but a lot of UAs
let you get away with it.)
Of course.
Don't use the same text for both a name -and- an id, less it is on the
same element.
Yes.


For example:
<textarea name="hi" id="hi"></textarea> is valid.
Garrett
 
T

Thomas 'PointedEars' Lahn

dhtml said:
There is no good reason not to assign a form control an ID.

There is at least one: MSHTML. There is also one good reason for assigning
a form control an ID: CSS. So where no CSS is involved, the `id' attribute
should not be used there.


PointedEars
 
T

Thomas 'PointedEars' Lahn

dhtml said:
There is no good reason not to assign a form control an ID.

There is at least one: MSHTML. There is also at least one good reason for
assigning a form control an ID: CSS.
It is often useful, for getElementById lookup,

Ask yourself: How often would using the more compatible `elements'
collection instead not have sufficed?
for explicit LABEL element to work.

ACK, required for meeting accessibility guidelines/legislation.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top