Clearing a textbox in a function call

Discussion in 'Javascript' started by threepins@gmail.com, Jan 30, 2005.

  1. Guest

    Howdy,

    Just wondering how the following might be done:

    I have a couple of different webpages that need to call a function to
    clear the textbox onFocus of the textbox. I'm wanting to use one
    function that I can call from a javascript include file (rather than
    having multiple versions of the function all through the site).

    So far i've tried the following:

    function clearfield(poForm,poElement)
    {
    if (document.forms[poForm].poElement.value == "Type Search Here...")
    document.forms[poForm].poElement.value = "";
    }

    where the passing page calls the function as:

    clearfield('formname','formelement');

    but just get errors.
    Any help on this one would be most appreciative!!

    Cheers
    Bj McGowan
    , Jan 30, 2005
    #1
    1. Advertising

  2. wrote:

    > I have a couple of different webpages that need to call a function to
    > clear the textbox onFocus of the textbox.


    whatever.js:

    function clrTxt(oElem, oEvent) {
    if (
    oElem
    && oElem.value
    && oElem.defaultValue
    && oEvent
    && oEvent.type
    ) {
    if (
    oEvent.type == "focus"
    && oElem.value == oElem.defaultValue
    ) {
    oElem.value = "";
    }
    else if (
    oEvent.type == "blur"
    && oElem.value == ""
    ) {
    oElem.value = oElem.defaultValue;
    }
    }
    }

    whatever.html:

    ....
    <script type="text/javascript" src="whatever.js"></script>
    ....
    <input ...
    onfocus="clrTxt(this, event)"
    onblur="clrTxt(this, event)"
    value="Type Search Here..."
    >

    ....

    ciao, dhgm
    Dietmar Meier, Jan 30, 2005
    #2
    1. Advertising

  3. Bj Guest

    Thanks Dietmar,

    Worked a treat!

    Bj

    "Dietmar Meier" <> wrote in
    message news:...
    > wrote:
    >
    > > I have a couple of different webpages that need to call a function to
    > > clear the textbox onFocus of the textbox.

    >
    > whatever.js:
    >
    > function clrTxt(oElem, oEvent) {
    > if (
    > oElem
    > && oElem.value
    > && oElem.defaultValue
    > && oEvent
    > && oEvent.type
    > ) {
    > if (
    > oEvent.type == "focus"
    > && oElem.value == oElem.defaultValue
    > ) {
    > oElem.value = "";
    > }
    > else if (
    > oEvent.type == "blur"
    > && oElem.value == ""
    > ) {
    > oElem.value = oElem.defaultValue;
    > }
    > }
    > }
    >
    > whatever.html:
    >
    > ...
    > <script type="text/javascript" src="whatever.js"></script>
    > ...
    > <input ...
    > onfocus="clrTxt(this, event)"
    > onblur="clrTxt(this, event)"
    > value="Type Search Here..."
    > >

    > ...
    >
    > ciao, dhgm
    Bj, Jan 31, 2005
    #3
  4. Bj wrote:

    > Worked a treat!


    Not really. Didn't test it before posting, and as I see now,
    there's a mistake:

    >> function clrTxt(oElem, oEvent) {
    >> if (
    >> oElem
    >> && oElem.value
    >> && oElem.defaultValue
    >> && oEvent
    >> && oEvent.type
    >> ) {


    should, of course, be replaced with:

    function clrTxt(oElem, oEvent) {
    if (
    oElem
    && typeof oElem.value != "undefined"
    && oElem.defaultValue
    && oEvent
    && oEvent.type
    ) {

    ciao, dhgm
    Dietmar Meier, Jan 31, 2005
    #4
    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. grbgooglefan
    Replies:
    2
    Views:
    423
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    442
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    392
    grbgooglefan
    Jan 30, 2008
  4. LeSam

    Clearing an ASP textbox on click

    LeSam, Apr 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    113
    LeSam
    Apr 6, 2005
  5. RelaxoRy
    Replies:
    2
    Views:
    90
    relaxory
    Apr 19, 2004
Loading...

Share This Page