473,398 Members | 2,188 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,398 software developers and data experts.

can't get checkbox to check

LRW
Very basic, very simple, but I can't get a checkbox to check when the
value of a text field changes to something greater than 0.

Here's what I have:

THE javascript:

<script language=JavaScript>
<!-- Begin
function sel_comm(comm_val)
{
if (comm_val > 0)
{
document.inhouse-f2.add_comm.checked = true;
}
}
// End -->
</script>

HTML:

<INPUT TYPE="checkbox" NAME="add_comm" ID="add_comm" VALUE="1">
Commission: <INPUT TYPE="text" NAME="item_comm" ID="item_comm"
size="6" onChange="sel_comm(this.value);">

I just don't get why it's not working. I must be missing something,
but I have no idea what. The FORM tag's NAME and ID elements are
"inhouse-f2". What else could it be?

Thanks for any ideas!
Liam
Jul 23 '05 #1
4 1674
In article <4c**************************@posting.google.com >,
me********@gmail.com enlightened us with...
Very basic, very simple, but I can't get a checkbox to check when the
value of a text field changes to something greater than 0.

Actually, not as simple as you'd think.

Textboxes are text. You're comparing text to a number. It's trying to convert
to compare, but what it does it is converts the number to text, so you get
the wrong kind of comparison.

Here's what I have:

THE javascript:

<script language=JavaScript>
type="text/javascript"
language is deprecated.
<!-- Begin
function sel_comm(comm_val)
{
if (comm_val > 0)
if (parseInt(comm_val,10) > 0)
<INPUT TYPE="checkbox" NAME="add_comm" ID="add_comm" VALUE="1">

Commission: <INPUT TYPE="text" NAME="item_comm" ID="item_comm"
size="6" onChange="sel_comm(this.value);">

Also note that the onChange event fires AFTER focus has left the element. I
don't know how good it is to use the 'this' keyword across browsers for that.
Anyone have any comments about that?

Oh, and I think value is undefined if there is no value in the box. So test
what happens if the user deletes the value and then moves focus. You may have
to test for 'not a number' conditions, too.

And your syntax in your function isn't cross-browser, either. If this is for
the internet, your best bet is
document.forms["inhouse-f2"].elements["add_comm"].checked = true;

HTH

--
--
~kaeli~
In democracy your vote counts. In feudalism your count votes.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
On Thu, 30 Sep 2004 14:32:47 -0500, kaeli <ti******@NOSPAM.comcast.net>
wrote:
In article <4c**************************@posting.google.com >,
me********@gmail.com enlightened us with...
[snip]
Textboxes are text. You're comparing text to a number. It's trying to
convert to compare, but what it does it is converts the number to text,
so you get the wrong kind of comparison.
No. Comparisons, with both relational and equality operators, are biased
towards numbers. If one operator is a number and the other is string, the
string is converted to a number, even if that conversion results in NaN.

[snip]
<!-- Begin
Don't try to hide scripts. It's not necessary any more. If you have some
special requirement, such as the page is parsed by software that can't
cope with scripts, place the script in an external file. Most scripts
should be separated from the document anyway.
function sel_comm(comm_val)
{
if (comm_val > 0)


if (parseInt(comm_val,10) > 0)


So in light of the above, that isn't needed. It won't do any harm, but
it's redundant. If some bugs I don't know of apply to such a comparison,
using

if((+comm_val) > 0) {

would be more efficient.
<INPUT TYPE="checkbox" NAME="add_comm" ID="add_comm" VALUE="1">


Commission: <INPUT TYPE="text" NAME="item_comm" ID="item_comm"
size="6" onChange="sel_comm(this.value);">

Also note that the onChange event fires AFTER focus has left the
element. I don't know how good it is to use the 'this' keyword across
browsers for that. Anyone have any comments about that?


The this operator always refers to the element targeted by the event. The
only exception I know of is when the proprietary attachEvent method is
used. Here, IE doesn't set the this operator correctly, so it refers to
the global object, instead.

If you want to add multiple event listeners in a cross browser way, you
can try:

<URL:http://www.mlwinter.pwp.blueyonder.co.uk/clj/dom-events.js>

It hasn't displayed any problems, but do let me know if you use it and
find any.
Oh, and I think value is undefined if there is no value in the box. So
test what happens if the user deletes the value and then moves focus.
You may have to test for 'not a number' conditions, too.
It should just be an empty string. In that case, the result of the
conversion will be 0.
And your syntax in your function isn't cross-browser, either. If this is
for the internet, your best bet is
document.forms["inhouse-f2"].elements["add_comm"].checked = true;


It's not just "good form" in this case; it's required.

document.inhouse-f2.add_comm.checked = true

should be treated thus:

1) f2.add_comm.checked is subtracted from document.inhouse
2) true is assigned to the result of the operation

Both steps are errors. "f2" and "inhouse" don't exist, so that will stop
execution of the script. If it somehow succeeded, the second step would
fail as a subtraction isn't a valid left-hand expression. This is the
source of the problem.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
In article <opse5o2suox13kvk@atlantis>, M.******@blueyonder.co.invalid
enlightened us with...

No. Comparisons, with both relational and equality operators, are biased
towards numbers. If one operator is a number and the other is string, the
string is converted to a number, even if that conversion results in NaN.


Then is parseInt really only needed when BOTH operands are strings and you
want a number?

TIA

--
--
~kaeli~
The definition of a will?... (It's a dead giveaway.)
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4
On Fri, 1 Oct 2004 08:39:17 -0500, kaeli <ti******@NOSPAM.comcast.net>
wrote:

[snip]
Then is parseInt really only needed when BOTH operands are strings and
you want a number?


Pretty much, yeah, otherwise you'd perform a lexical string comparison.
null and false convert to 0, true converts to 1, and undefined converts to
NaN. Objects also attempt to convert to a number, but if they can't, I
doubt forcing a conversion would do any good.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fred | last post by:
Hi, I defined a form consisting of checkboxes like: <form> <input type="checkbox" name=ck id=ck onclick="check(this.form)" <input type="checkbox" name=ck id=ck onclick="check(this.form)" ........
3
by: Jack | last post by:
<i><input type="checkbox" name="chk_Complete" value="TRUE" <%Response.Write l_IsChecked%>"<%if cbool(l_IsChecked) then Response.Write " checked" Else Response.Write " unchecked"%>> The above...
4
by: feanor | last post by:
I need to select children checkboxes when selecting the parent one. This is my function: function SelectChildrens(checkbox_name){ form = document.forms; Sname = checkbox_name.split("-"); for...
1
by: iforsyth | last post by:
Have a paging datagrid with a checkbox control in column(0). ViewState is enabled. I check the checkbox in first row of the grid on a page and then the program hits this event: Private Sub...
6
by: hazz | last post by:
ICollection CreateDataSource() { dt.Columns.Add(new DataColumn("ReportInclude", typeof(Boolean))); for (int i=0; i<=10; i++) { dr = dt.NewRow(); dr = 0; dt.Rows.Add(dr); } void...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
2
by: Zvonko Bičkup | last post by:
Hi! I have 5 checkboxes with the same name: <input type="checkbox" name="check" value="XYZ" /> <input type="checkbox" name="check" value="ZXY" /> <input type="checkbox" name="check" value="MHG"...
3
by: Mahathi | last post by:
Hi I have a small problem in maintaining the state of a check box. Please do me a favour by telling me the procedure how to do that. My requirement is that "I have to map some roles with...
2
by: =?Utf-8?B?UmljaA==?= | last post by:
Is there a cancel argument for cancelling if you want to check or uncheck a checkbox? In the checkChanged event of a checkbox I ask the user if they are sure they want to check/uncheck...
9
by: raamay | last post by:
I have six checkboxes as shown below: <table> <tr> <td><input name="spec1" type="checkbox" value="0" tabindex="11" /><label id="label">Bridge Construction</label></td> </tr> <tr> <td><input...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.