Combining two functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luis

    #1

    Combining two functions

    Is there a way to combine the following ShowName()
    functions so that I can just call one function on the onblur event of
    the text field?


    <html>
    <head>
    <script language="JavaS cript">
    function ShowName1() {
    alert "You were on Field1"
    }

    function ShowName2() {
    alert "You were on Field1"
    }
    </script>
    </head>

    <body>
    <input type="text" name="Field1" onblur="ShowNam e1()">
    <input type="text" name="Field2" onblur="ShowNam e2()">
    </body>
    </html>

    My first thought is to establish which field had the focus and then pass
    that field name to the function. But how do I determine which field had
    the focus?

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Ivo

    #2
    Re: Combining two functions

    "Luis" wrote
    <snip>[color=blue]
    > My first thought is to establish which field had the focus and then pass
    > that field name to the function. But how do I determine which field had
    > the focus?[/color]

    The easiest way is probably to use the "this" keyword:

    <html>
    <head>
    <script type="text/javascript">
    function ShowName(n) {
    alert ("You were on "+n.name);
    }
    </script>
    </head>

    <body>
    <input type="text" name="Field1" onblur="ShowNam e(this)">
    <input type="text" name="Field2" onblur="ShowNam e(this)">
    </body>
    </html>


    Comment

    • Andy Maurer

      #3
      Re: Combining two functions

      Luis <andyzaNOSPAM@w ebmail.co.za> wrote in message news:<41342038$ 0$14444$c397aba @news.newsgroup s.ws>...[color=blue]
      > Is there a way to combine the following ShowName()
      > functions so that I can just call one function on the onblur event of
      > the text field?
      >
      >
      > <html>
      > <head>
      > <script language="JavaS cript">
      > function ShowName1() {
      > alert "You were on Field1"
      > }
      >
      > function ShowName2() {
      > alert "You were on Field1"
      > }
      > </script>
      > </head>
      >
      > <body>
      > <input type="text" name="Field1" onblur="ShowNam e1()">
      > <input type="text" name="Field2" onblur="ShowNam e2()">
      > </body>
      > </html>
      >
      > My first thought is to establish which field had the focus and then pass
      > that field name to the function. But how do I determine which field had
      > the focus?
      >
      > *** Sent via Developersdex http://www.developersdex.com ***
      > Don't just participate in USENET...get rewarded for it![/color]

      Hi,

      simply pass an argument to your function.

      ....onblur="Sho wName1("1")"> // make sure, the "" are correct!!

      in the function you use:

      function ShowName1(x) {
      // of course you can use a switch - case statement and/or some if's to
      get the appropriate actions
      alert ("you've been in field " +x);
      }

      HTH,

      Andy

      Comment

      • Michael Winter

        #4
        Re: Combining two functions

        On 31 Aug 2004 04:16:15 -0700, Andy Maurer <andreas.maurer 1971@web.de>
        wrote:

        [snip]
        [color=blue]
        > ...onblur="Show Name1("1")"> // make sure, the "" are correct!![/color]

        You want those to be single quotes ('):

        ...onblur="Show Name1('1')">

        or entity references:

        ...onblur="Show Name1(&quot;1&q uot;)">

        [snip]

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        Working...