Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Need Help With My Code to Select Contents of Textbox

Reply
Thread Tools

Need Help With My Code to Select Contents of Textbox

 
 
Mad Ape
Guest
Posts: n/a
 
      12-05-2008
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>

++++++++++++++++++++++++++++++++++++++++++++++++
 
Reply With Quote
 
 
 
 
Mad Ape
Guest
Posts: n/a
 
      12-05-2008
Mad Ape wrote:
> 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
 
Reply With Quote
 
 
 
 
SAM
Guest
Posts: n/a
 
      12-05-2008
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 ...


--
sm
 
Reply With Quote
 
Jeremy J Starcher
Guest
Posts: n/a
 
      12-05-2008
On Fri, 05 Dec 2008 19:58:41 +0100, SAM wrote:

> 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.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-06-2008
Mad Ape wrote:
> <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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
dhtml
Guest
Posts: n/a
 
      12-07-2008
Jeremy J Starcher wrote:
> On Fri, 05 Dec 2008 19:58:41 +0100, SAM wrote:
>
>> 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.)
>


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

--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-07-2008
dhtml wrote:
> Jeremy J Starcher wrote:
>> 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.


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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-07-2008
dhtml wrote:
> Jeremy J Starcher wrote:
>> 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.


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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
master select list changes contents of slave select lists Dan Thomas Ruby 0 02-02-2011 06:59 PM
Adding contents on yaml file without overwriting actual contents Kamarulnizam Rahim Ruby 4 01-28-2011 09:10 AM
if innerHTML used twice then it replaces the contents i want it to display all the contents virendra.amritkar@gmail.com Javascript 0 06-29-2007 08:13 AM
Can I restrict both attribute contents and element contents in schema Don Adams XML 1 03-05-2004 12:48 PM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM



Advertisments