Combining two functions

L

Luis

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="JavaScript">
function ShowName1() {
alert "You were on Field1"
}

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

<body>
<input type="text" name="Field1" onblur="ShowName1()">
<input type="text" name="Field2" onblur="ShowName2()">
</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?
 
I

Ivo

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?

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="ShowName(this)">
<input type="text" name="Field2" onblur="ShowName(this)">
</body>
</html>
 
A

Andy Maurer

Luis said:
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="JavaScript">
function ShowName1() {
alert "You were on Field1"
}

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

<body>
<input type="text" name="Field1" onblur="ShowName1()">
<input type="text" name="Field2" onblur="ShowName2()">
</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?

Hi,

simply pass an argument to your function.

....onblur="ShowName1("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
 
M

Michael Winter

On 31 Aug 2004 04:16:15 -0700, Andy Maurer <[email protected]>
wrote:

[snip]
...onblur="ShowName1("1")"> // make sure, the "" are correct!!

You want those to be single quotes ('):

...onblur="ShowName1('1')">

or entity references:

...onblur="ShowName1(&quot;1&quot;)">

[snip]

Mike
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top