Clearing a textbox in a function call

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • threepins@gmail.com

    #1

    Clearing a textbox in a function call

    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(poFo rm,poElement)
    {
    if (document.forms[poForm].poElement.valu e == "Type Search Here...")
    document.forms[poForm].poElement.valu e = "";
    }

    where the passing page calls the function as:

    clearfield('for mname','formele ment');

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

    Cheers
    Bj McGowan

  • Dietmar Meier

    #2
    Re: Clearing a textbox in a function call

    threepins@gmail .com wrote:
    [color=blue]
    > I have a couple of different webpages that need to call a function to
    > clear the textbox onFocus of the textbox.[/color]

    whatever.js:

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

    whatever.html:

    ....
    <script type="text/javascript" src="whatever.j s"></script>
    ....
    <input ...
    onfocus="clrTxt (this, event)"
    onblur="clrTxt( this, event)"
    value="Type Search Here..."[color=blue]
    >[/color]
    ....

    ciao, dhgm

    Comment

    • Bj

      #3
      Re: Clearing a textbox in a function call

      Thanks Dietmar,

      Worked a treat!

      Bj

      "Dietmar Meier" <usereplytoinst ead@innoline-systemtechnik.d e> wrote in
      message news:3642tmF4r6 i0hU1@individua l.net...[color=blue]
      > threepins@gmail .com wrote:
      >[color=green]
      > > I have a couple of different webpages that need to call a function to
      > > clear the textbox onFocus of the textbox.[/color]
      >
      > whatever.js:
      >
      > function clrTxt(oElem, oEvent) {
      > if (
      > oElem
      > && oElem.value
      > && oElem.defaultVa lue
      > && oEvent
      > && oEvent.type
      > ) {
      > if (
      > oEvent.type == "focus"
      > && oElem.value == oElem.defaultVa lue
      > ) {
      > oElem.value = "";
      > }
      > else if (
      > oEvent.type == "blur"
      > && oElem.value == ""
      > ) {
      > oElem.value = oElem.defaultVa lue;
      > }
      > }
      > }
      >
      > whatever.html:
      >
      > ...
      > <script type="text/javascript" src="whatever.j s"></script>
      > ...
      > <input ...
      > onfocus="clrTxt (this, event)"
      > onblur="clrTxt( this, event)"
      > value="Type Search Here..."[color=green]
      > >[/color]
      > ...
      >
      > ciao, dhgm[/color]


      Comment

      • Dietmar Meier

        #4
        Re: Clearing a textbox in a function call

        Bj wrote:
        [color=blue]
        > Worked a treat![/color]

        Not really. Didn't test it before posting, and as I see now,
        there's a mistake:
        [color=blue][color=green]
        >> function clrTxt(oElem, oEvent) {
        >> if (
        >> oElem
        >> && oElem.value
        >> && oElem.defaultVa lue
        >> && oEvent
        >> && oEvent.type
        >> ) {[/color][/color]

        should, of course, be replaced with:

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

        ciao, dhgm

        Comment

        Working...