Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Need Help With My Code to Select Contents of Textbox (http://www.velocityreviews.com/forums/t937809-need-help-with-my-code-to-select-contents-of-textbox.html)

Mad Ape 12-05-2008 06:14 PM

Need Help With My Code to Select Contents of Textbox
 
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>

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

Mad Ape 12-05-2008 06:16 PM

Re: Need Help With My Code to Select Contents of Textbox
 
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

SAM 12-05-2008 06:58 PM

Re: Need Help With My Code to Select Contents of Textbox
 
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

Jeremy J Starcher 12-05-2008 08:08 PM

Re: Need Help With My Code to Select Contents of Textbox
 
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.



Thomas 'PointedEars' Lahn 12-06-2008 12:10 PM

Re: Need Help With My Code to Select Contents of Textbox
 
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

dhtml 12-07-2008 12:59 AM

Re: Need Help With My Code to Select Contents of Textbox
 
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/ >

Thomas 'PointedEars' Lahn 12-07-2008 09:30 AM

Re: Need Help With My Code to Select Contents of Textbox
 
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$8300dec7@news.demon.co.uk>

Thomas 'PointedEars' Lahn 12-07-2008 09:35 AM

Re: Need Help With My Code to Select Contents of Textbox
 
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$8300dec7@news.demon.co.uk>


All times are GMT. The time now is 07:35 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.