Need Help With My Code to Select Contents of Textbox

Discussion in 'Javascript' started by Mad Ape, Dec 5, 2008.

  1. Mad Ape

    Mad Ape Guest

    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, Dec 5, 2008
    #1
    1. Advertising

  2. Mad Ape

    Mad Ape Guest

    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
    Mad Ape, Dec 5, 2008
    #2
    1. Advertising

  3. Mad Ape

    SAM Guest

    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
    SAM, Dec 5, 2008
    #3
  4. 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.
    Jeremy J Starcher, Dec 5, 2008
    #4
  5. 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
    Thomas 'PointedEars' Lahn, Dec 6, 2008
    #5
  6. Mad Ape

    dhtml Guest

    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/ >
    dhtml, Dec 7, 2008
    #6
  7. 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$>
    Thomas 'PointedEars' Lahn, Dec 7, 2008
    #7
  8. 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$>
    Thomas 'PointedEars' Lahn, Dec 7, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Adams
    Replies:
    1
    Views:
    592
    Martin Honnen
    Mar 5, 2004
  2. Kamarulnizam Rahim
    Replies:
    4
    Views:
    216
    Robert Klemme
    Jan 28, 2011
  3. Dan Thomas
    Replies:
    0
    Views:
    273
    Dan Thomas
    Feb 2, 2011
  4. palmiere
    Replies:
    1
    Views:
    396
    Erwin Moller
    Feb 9, 2004
  5. Replies:
    0
    Views:
    189
Loading...

Share This Page