Forum Moderators: open

Message Too Old, No Replies

VS functions not working

         

Butch Cassidy

1:11 am on May 20, 2006 (gmt 0)

10+ Year Member



Belos are excerpts from two pages in my web site. In the first one, the VS functions are not being called on the approppriate events. In the second, the functions are being called normally. I've compared these two pages for two days now and can't see any differences between them. Can someone see what I am missing?

Note: most of the identifying information has been removed from the pages, but their behavior in a browser matches the original, unedited, versions.

Sample 1
*************************************
<html>
<head>
<title>
VS functions don't work on this page
</title>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script>
function changefunc ()
{
alert('in changefunc');
}
function dummyfunc ()
{
alert('in dummyfunc');
return false;
}
function chcount (theform)
{
alert('in chcount');
curcount = theform.curCount.value;
newcount = theform.repeatCount.value;
if (curcount!= newcount)
{
// The new value must be a non-negative integer.
try
{
if newcount < 0
{
alert("The repeat count must be a non-negative integer.");
theform.reset();
return false;
}
}
catch (err)
{
// The value couldn't be used as a number.
alert(err + "The repeat count must be a non-negative integer.");
theform.reset();
return false;
}
}
else
{
// The count hasn't changed; don't send.
return false;
}
// If we got here, we have a good new repeat count; send it.
return true;
}
</script>
</head>
<body>

<h1 align="left">
<b>
<font face="Arial" color="#000080" size="6">
VS functions don't work on this page
</font>
</b>
</h1>
<hr />
<table border=0>

<form method=POST action="sample1.html" name="sample1">
<tr valign=top>
<td align=left>
Input 1
</td>
<td align=center>

<input type="text" onchange="changefunc()" name="repeatCount" size="6" maxlength="256" value="1" />

</td>
<td align=center>
<input type=hidden name=curCount value="1">
<input type=submit name="useraction" value="Change Count" onclick="return chcount(this.form);">
</td>
</tr>
</form>

<form method=POST action="sample1.html" name="sample1">
<tr valign=top>
<td align=left>
</td>
<td align=center>
</td>
<td align=center>
<input type=submit name="useraction" value="Dummy button" onclick="return dummyfunc();">
</td>
</tr>
</form>
</table>
</body>
</html>
*************************************

Sample 2
*************************************
<html>
<head>
<title>
VS functions work on this page
</title>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script>
function changefunc (theform)
{
alert('in changefunc');
}
function chtype (theform)
{
alert('in chtype');
curtype = theform.curType.value;
for (var i = 0; i < theform.imagerType.length; i++)
{
if (theform.imagerType[i].checked &&
theform.imagerType[i].defaultChecked)
{
// The imager type hasn't changed; don't send the data
return false;
}
}
// The imager type has been changed; confirm the change
if (confirm("Changing this data has no effect, since \n" +
"this is a dummy page.\n" +
"Proceed?"))
{
return true;
}

theform.reset();
return false;
}

function chmode (theform)
{
alert('in chmode');
curmode = theform.curMode.value;
optnum = theform.imagerMode.selectedIndex;
newmode = theform.imagerMode.options[optnum].value;
if (curmode!= newmode)
{
return true;
}
else
{
return false;
}
}
</script>
</head>
<body>

<h1 align="left">
<b>
<font face="Arial" color="#000080" size="6">
VS functions work on this page
</font>
</b>
</h1>
<table border=0>

<form method=POST action="sample2.html" name="sample2">
<tr valign=top>
<td align=left>
input 1
</td>
<td align=center>
<input type=radio onchange="changefunc()" name=imagerType value="1" checked='checked'>type1<input type=radio onchange="changefunc()" name=imagerType value="2">type2
</td>
<td align=center>
<input type=hidden name=curType value="1">
<input type=submit name="useraction" value="Change Type" onclick="return chtype(this.form);">
</td>
</tr>
<tr valign=top>
<td align=left>
input 2
</td>
<td align=center>
<select name="imagerMode" onchange="changefunc()">
<option value='1'>Option 1</option><option value='2'>Option 2</option><option value='3' selected='selected'>Option 3</option>
</select>
</td>
<td align=center>
<input type=hidden name=curMode value="3">
<input type=submit name="useraction" value="Change Mode" onclick="return chmode(this.form);">
</td>
</tr>
</form>


</table>

</body>
</html>
*************************************

Thanks in advance for any help you can offer.

Barry Carroll

[edited by: jatar_k at 1:13 am (utc) on May 20, 2006]
[edit reason] no sigs thanks [/edit]

RonPK

1:49 pm on May 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Barry, in your first sample you're using

<input type="text" onchange="changefunc()" [..]

Text input fields don't know the

onchange
event. Try using
onkeyup
instead.

Butch Cassidy

11:31 pm on May 24, 2006 (gmt 0)

10+ Year Member



Thanks for your reply, but I'm still confused. JavaScript: The Definitive Guide, 4th Edition contains the following:


Text.onchange
the handler invoked when input value changes

---------------------------------------------------------

Availability
JavaScript 1.0

Synopsis
<input type="text" onchange="handler" ... >
text.onchange

Description
The onchange property of a Text element refers to an event handler function that is invoked when the user changes the value in the input field and then "commits" those changes by moving keyboard focus (i.e., by clicking the mouse elsewhere or pressing Tab or Return).

Is this wrong?

Barry

RonPK

10:33 am on May 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry about that. Flanagan is right, as usual. I was referring to the confusing fact that onchange is not triggered when you're still typing in the text field.

You do have a syntax error in the script on the first page:

if newcount < 0
. If-conditions always need to be enclosed by ( and ).
if (newcount < 0)

Butch Cassidy

6:49 pm on May 25, 2006 (gmt 0)

10+ Year Member



That's it! The validation function is working now.

I knew it had to be some simple programmer error. I've been writing Python code for the last year, where parens in if statements are actively discouraged wherever possible.

Thanks for your help.

Barry