firefox not firing onclick

R

realraven2000

Hi,

my colleague coded the following onclick events with Javascript which
only seem to fire in IE not in firefox. The checkboxes are built in
Form frmProdDet while looping through a recordset (ncount=0..1..2..n)
of Stock Items. The idea is to prefill quantity of ordered items with 1
when buy checkbox is checked, and to empty it when cleared. I put in
alert for testing displays in IE fine, not in fx. My guess its a
syntactical problem.

Any idea why this is not firing in firefox?

apart from this the site works well in both MSIE and ff. According to
him it does not have to be Mozilla compatible, but it would be nicer
for me (as I do most of my testing with ff first).

thanks in advance.
Axel

Relevant code:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<script language="JavaScript" type="text/JavaScript">
<!--
function chkbuy(ncount)
{
//alert
alert("checked=" + document.frmProdDet("checkbox" + ncount).checked);
if (document.frmProdDet("checkbox" + ncount).checked==true)
{
document.frmProdDet("txtQty" + ncount).value="1";
}
else
{
document.frmProdDet("txtQty" + ncount).value="";
}
}
//-->
</script>

<form Name="frmProdDet"  Method="post">

<table><tr>


<td width="30"> <div align="left">
<input type="checkbox" name="checkbox<%=ncount%>"
onclick=Javascript:chkbuy(<%=ncount%>)>
</div></td>
<td width="31"> <input name="txtQty<%=ncount%>" type="text"
class="bodysmall" size="2">
</td>

</table></tr>


</form>
 
M

Martin Honnen

alert("checked=" + document.frmProdDet("checkbox" + ncount).checked);
^^^^^^^^^^^^^^^^^^^^
That is a function call, I am sure you get a script error in the Firefox
JavaScript console. If you want to access a property of an object then use
document.frmProdDet["checkbox" + ncount]

However it appears easier to simply use
<input onclick="chkbuy(this);"
and then in the function you can directly process the input e.g.
function chkbuy (input) {
if (input.checked) {
 
L

Lee

(e-mail address removed) said:
Hi,

my colleague coded the following onclick events with Javascript which
only seem to fire in IE not in firefox. The checkboxes are built in
Form frmProdDet while looping through a recordset (ncount=0..1..2..n)
of Stock Items. The idea is to prefill quantity of ordered items with 1
when buy checkbox is checked, and to empty it when cleared. I put in
alert for testing displays in IE fine, not in fx. My guess its a
syntactical problem.

Any idea why this is not firing in firefox?

The syntax is wrong in a way that IE tolerates.
The references to form elements should be as below.
Also, you don't need the "<!--" and "//-->" comments
and you don't need to compare a Boolean value to true.


<script language="JavaScript" type="text/JavaScript">
function chkbuy(ncount)
{
//alert
alert("checked=" + document.frmProdDet.elements["checkbox" + count].checked);
if (document.frmProdDet.elements["checkbox" + ncount].checked)
{
document.frmProdDet.elements["txtQty" + ncount].value="1";
}
else
{
document.frmProdDet.elements["txtQty" + ncount].value="";
}
}
</script>
 
M

Michael Winter

On 06/07/2005 17:24, (e-mail address removed) wrote:

[snip]
<script language="JavaScript" type="text/JavaScript">
<!--

Drop the language attribute and the SGML comments.

[snip]
alert("checked=" + document.frmProdDet("checkbox" + ncount).checked);

A form object is not a function, so calling it will only cause an error.
Use square brackets:

alert('checked=' + document.frmProdDet['checkbox' + ncount].checked);

The same applies later. However, you can go one step further by using
the forms and elements collections, as well as saving a reference to the
form, rather than resolving it more than once:

function chkbuy(ncount) {
var elements = document.forms.frmProdDet.elements;

if(elements['checkbox' + ncount].checked) {
elements['txtQty' + ncount].value = '1';
} else {
elements['txtQty' + ncount].value = '';
}
}

or even better (variables truncated to avoid wrapping):

function chkbuy(n) {
var e = document.forms.frmProdDet.elements;

e['txtQty' + n].value = e['checkbox' + n].checked
? '1'
: '';
}

[snip]
<input type="checkbox" name="checkbox<%=ncount%>"
onclick=Javascript:chkbuy(<%=ncount%>)>

That onclick attribute must be quoted. You can also drop the Javascript:
prefix unless you're using client-side VBScript elsewhere (in which
case, compatibility with Firefox is rather pointless :).

Hope that helps,
Mike
 
R

realraven2000

Thanks Mike,

lovely code. Didn't know you could use single quotes for strings as
well. But I like the use of the ? : operator reminds me of the times
when I was still programming C++ (bliss). I liked the suggestion using
'this' as well, but it doesn't quite work as I still would have to
reference the textbox and end up passing two references or ending up
with a mish mash. Not using the form.elements collection, in case its
to oblique for my colleage. But I am a JScript newbie myself wish I
could replace all the serverside VBScript for Java as well, but too
rusty - hence no exercise, no cigar.

This is the final function:

function chkbuy(ncount)
{
document.frmProdDet["txtQty" + ncount].value =
(document.frmProdDet["checkbox" + ncount].checked) ? "1" : "";
}
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top