469,328 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

firefox not firing onclick

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:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3.  
  4. <script language="JavaScript" type="text/JavaScript">
  5. <!--
  6. function chkbuy(ncount)
  7. {
  8. //alert
  9. alert("checked=" + document.frmProdDet("checkbox" + ncount).checked);
  10. if (document.frmProdDet("checkbox" + ncount).checked==true)
  11. {
  12. document.frmProdDet("txtQty" + ncount).value="1";
  13. }
  14. else
  15. {
  16. document.frmProdDet("txtQty" + ncount).value="";
  17. }
  18. }
  19. //-->
  20. </script>
  21.  
  22. <form Name="frmProdDet"  Method="post">
  23.  
  24. <table><tr>
  25.  
  26.  
  27. <td width="30"> <div align="left">
  28. <input type="checkbox" name="checkbox<%=ncount%>"
  29. onclick=javascript:chkbuy(<%=ncount%>)>
  30. </div></td>
  31. <td width="31"> <input name="txtQty<%=ncount%>" type="text"
  32. class="bodysmall" size="2">
  33. </td>
  34.  
  35. </table></tr>
  36.  
  37.  
  38. </form>
  39.  
Jul 23 '05 #1
4 17817


re***********@hotmail.com wrote:

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) {

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
On 06/07/2005 17:24, re***********@hotmail.com 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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 23 '05 #3
Lee
re***********@hotmail.com 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>

Jul 23 '05 #4
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" : "";
}

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Stuart Perryman | last post: by
4 posts views Thread by the other john | last post: by
2 posts views Thread by =?Utf-8?B?Uml0YUc=?= | last post: by
3 posts views Thread by Jay | last post: by
21 posts views Thread by brucedodds | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.