473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 17982


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stuart Perryman | last post by:
Hi, I have the following code which works just fine in IE6 but not in Firefox. It is an extract of several table rows each with an individual form. It is generated by php. <form...
3
by: usenet | last post by:
Hi All, I have some initialization to be done at page load time, which changes the text of some anchors so that they are consistent with the query string (these anchors are used as criteria...
4
by: the other john | last post by:
I came across a third party script I want to learn how to configure as well as learn more dhtml in the doing. I'm not much of a JS guy yet but I'm working on it. This script works fine in IE6...
2
by: =?Utf-8?B?Uml0YUc=?= | last post by:
I posted this question in the C# discussion group but am posting it here also since ASP is involved. I'm new to C# and need some help regarding an onClick event not firing. I have a data grid...
11
by: ft310 | last post by:
Use Internet Explorer go to http://rhodeisland-aa.org/tester/meetings/index.htm On the left click "and go to meetings" Click any address in the right hand 'location' column. The entire page...
3
by: Jay | last post by:
I am on the 2.0 framework and have run the c:\windows\microsoft.net \framework\v1.1.4322\aspnet_regiis.exe -c and had no success. About half of the buttons on my webforms are firing and the other...
9
Basharat
by: Basharat | last post by:
Hi all I have problem on firing "onmouseleave" event of <div> html element. Here is the code im using: <div ID="BSHelpPanel" class="PageMenuMain" onclick="javascript:this.style.display='none';"...
3
by: sacha4 | last post by:
Hi, I need some help with my onClick event. I realise that there is some problem with the javascript but what exactly it is i am not able to know. <input type="Button" onClick="location = ''...
21
by: brucedodds | last post by:
I have an Access 2003 form bound to a SQL Server table via ODBC. Often clicking a button on the form has the effect of requerying the form rather than firing the OnClick event. Does anyone have...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.