473,889 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=JavaSc ript>
<!-- Begin
function sel_comm(comm_v al)
{
if (comm_val > 0)
{
document.inhous e-f2.add_comm.che cked = 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_c omm(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 1698
In article <4c************ **************@ posting.google. com>,
me********@gmai l.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=JavaSc ript>
type="text/javascript"
language is deprecated.
<!-- Begin
function sel_comm(comm_v al)
{
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_c omm(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******@NOSPA M.comcast.net>
wrote:
In article <4c************ **************@ posting.google. com>,
me********@gmai l.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_v al)
{
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_c omm(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.pw p.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.inhous e-f2.add_comm.che cked = true

should be treated thus:

1) f2.add_comm.che cked is subtracted from document.inhous e
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 <opse5o2suox13k vk@atlantis>, M.******@blueyo nder.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******@NOSPA M.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
1902
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)" ..... <input type="checkbox" name=ck id=ck onclick="check(this.form)" </form>
3
1990
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 code shows a checkbox on a asp form where the checkbox value is stored and retrived from a field in an Access database. When the checkbox is unchecked, for some reason, the value of checkbox shows null value instead of showing False
4
3430
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 (i=0;i<form.elements.length;i++){ THATname = form.elements.name.split("-"); if (Sname.length==1){ if (THATname==Sname){
1
4163
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 dgRegGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgRegGrid.PageIndexChanged I then do a loop to check the checkbox state.
6
2973
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 Page_Load(Object sender, EventArgs e) { ItemsGrid.DataSource = CreateDataSource(); void Item_Bound(Object sender, DataGridItemEventArgs e) {
10
5228
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. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named 'CheckAllRows' on the CheckedChanged event of this CheckBox. The CheckBox in the Header exists within the HeaderTemplate of a TemplateColumn in the...
2
3283
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" /> <input type="checkbox" name="check" value="IOJ" /> <input type="checkbox" name="check" value="BGT" /> How to check if at least one is checked?
3
2918
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 that of the users of the project. I have used checkboxes for selecting the roles that a particular user has. For example, an adminstrator has all roles in an organisation. Similarly an Employee has limited roles. Here let us take administrator...
2
21070
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 something. If No, I want to cancel the check or uncheck and cancel the click event. Right now I have a form level boolean checkvar that I set based on the user's response. If No then when entering the click event I don't perform various actions. Is...
9
3343
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 name="spec2" type="checkbox" value="1" tabindex="12" /><label id="label">Building Construction</label></td> </tr> <tr>
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10887
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7991
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5825
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.