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

Checkbox: programmatically checking it, triggering the 'onchange' handler


Hello,

I've been looking on the web for a solution to this problem:
I create a set of checkboxes, and 2 buttons:
- one is labeled "All"
- the other is labeled "None"

Clicking "All" is supposed to check all the checkboxes, which it does
(that's not rocket science ;), but the 'onchange' event does not get
triggered.

I couldn't find an answer to this issue in the archives of
c.l.j. (even though I bet there is), so any idea/pointer is absolutely
welcome.

Am I doing something inappropriate here?

Thank you for any info!

Best regards,

Arnaud

Nov 3 '05 #1
13 31098
Observe the rule of KIS,

;-)

<form>

<input type="checkbox" name="checkIt" />
<input type="checkbox" name="checkIt" />
<input type="checkbox" name="checkIt" />

<input type="button" value="Tick All Boxs" onclick="runCheckAll()" />

</form>

<script type="text/javascript">
<!--
function runCheckAll()
{

var changeCheck = document.getElementsByName('checkIt');
for(i=0; i<changeCheck.length; i++)
{
changeCheck[i].checked = true;
}

}
//-->
</script>

Nov 3 '05 #2
Ah wait, I didn't read your post properly, sorry.

I think you can set a handler to fire from a script without user
changing anything. Waitasec...

Nov 3 '05 #3
VK
aundro wrote:
Hello,

I've been looking on the web for a solution to this problem:
I create a set of checkboxes, and 2 buttons:
- one is labeled "All"
- the other is labeled "None"

Clicking "All" is supposed to check all the checkboxes, which it does
(that's not rocket science ;), but the 'onchange' event does not get
triggered.
Maybe because it doesn't have to? :-)
<checkbox> has one top level user input event: "onclick" (besides all
these onkeypress, onbeforeprint etc.)

IE has new event called "onpropertychange" which will be called if you
change the checkbox state:

<input type="checkbox" name="cb02" onpropertychange="myFunction(this)">

FF has some very obscure interface for programmatically generated
events discussed in my post here:

Posted by: VK
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/2602a35ef4a14ac1/50a2ac2da48b4df3>

But in this particular case an "artificial click" seems has no effect
(did not check this option through).

Am I doing something inappropriate here?


You can do whatever you want in any way you want :-)
I am just not clear why you need this two-tier scheme: 1) select 2)
trig onselect event to do something other ?
Why not just do it in one step:

checkboxes[i].checked = true;
someOtherActionWith(checkboxes[i]);

?

Nov 3 '05 #4
"VK" <sc**********@yahoo.com> writes:
Why not just do it in one step:

checkboxes[i].checked = true;
someOtherActionWith(checkboxes[i]);


Well, that's the way I'm proceeding right now, while I was waiting for
a possible better solution but, as nobody seems to be really inspired,
I guess there are none.

Also this post was, I admit, a bit oriented towards a more general
"Can someone tell/teach me about 'simulated' events?" ;)
(I honestly though programmatically checking a checkbox would trigger
it's onchange handler)

Still: Thank you very much for the info!

Arnaud

Nov 4 '05 #5
VK
> "VK" <sc**********@yahoo.com> writes:
Why not just do it in one step:

checkboxes[i].checked = true;
someOtherActionWith(checkboxes[i]);

aundro wrote: Well, that's the way I'm proceeding right now, while I was waiting for
a possible better solution but, as nobody seems to be really inspired,
I guess there are none.

Also this post was, I admit, a bit oriented towards a more general
"Can someone tell/teach me about 'simulated' events?" ;)
(I honestly though programmatically checking a checkbox would trigger
it's onchange handler)


As I said your question as posted was "How to trig an event from an
element if such element is not native EventProducer of the event in
question?".

Checkbox is not EventProducer of the "onchange" event (<select> is).
Theoretically you can create any artificial event using
document.createEvent (Gesko) and document.cretateEventObject (IE)
methods. But trying to dispatch artificial event to an element in
violation of its EventProducer model *seems* to lead to the security
exeption and script abort in FireFox.
As the question indeed doesn't seem very inspiring (it's rather QA
testing than an actual task) I don't feel like exploring it to the full
depth. Nevertheless the posted link shows all necessary technique to
program artificial events and to dispatch / fire them onto different
object. If you take your time to explore it - it would be interesting
to know the test results.

Nov 4 '05 #6
VK wrote:
"VK" <sc**********@yahoo.com> writes:

Why not just do it in one step:

checkboxes[i].checked = true;
someOtherActionWith(checkboxes[i]);

aundro wrote:
Well, that's the way I'm proceeding right now, while I was waiting for
a possible better solution but, as nobody seems to be really inspired,
I guess there are none.

Also this post was, I admit, a bit oriented towards a more general
"Can someone tell/teach me about 'simulated' events?" ;)
(I honestly though programmatically checking a checkbox would trigger
it's onchange handler)

As I said your question as posted was "How to trig an event from an
element if such element is not native EventProducer of the event in
question?".

Checkbox is not EventProducer of the "onchange" event (<select> is).


Checkboxes *do* have an onchange event, it's in the HTML 4 specification.

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-INPUT>

Implementations vary - IE doesn't fire the event until the checkbox
loses focus (as per the spec but unintuitive), Firefox /et al/ fire the
event when the checkbox is checked (essentially making it an onclick
event) which is not what the spec says to do but is more intuitive.

[...]
--
Rob
Nov 4 '05 #7
RobG wrote:
VK wrote:
As I said your question as posted was "How to trig an event from an
element if such element is not native EventProducer of the event in
question?".

Checkbox is not EventProducer of the "onchange" event (<select> is).
Checkboxes *do* have an onchange event,


There is no `onchange' event. There is a `change' event, and an
intrinsic `onchange' event handler.
it's in the HTML 4 specification.

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-INPUT>


The relevant specifications here are W3C DOM Level 2+ Events:

<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents>
<http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-eventgroupings-htmlevents>
PointedEars
Nov 4 '05 #8
Thomas 'PointedEars' Lahn wrote:
RobG wrote:

VK wrote:
As I said your question as posted was "How to trig an event from an
element if such element is not native EventProducer of the event in
question?".

Checkbox is not EventProducer of the "onchange" event (<select> is).
Checkboxes *do* have an onchange event,

There is no `onchange' event. There is a `change' event, and an
intrinsic `onchange' event handler.


OK, I was not spot-on with my terminology. :-x

The link I quoted is to the definition for input elements, of which
'checkbox' is a type. It shows that they have an onchange attribute,
the content of which is 'script'.

I really didn't know what to make of 'EventProducer of the "onchange"
event'.
it's in the HTML 4 specification.

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-INPUT>

The relevant specifications here are W3C DOM Level 2+ Events:

<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents>
<http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-eventgroupings-htmlevents>


You can go to the event spec and see what elements they apply to, or
look at the elements to see what attributes they have to find out what
events they support. Six of one, half a dozen of the other to me, but
thanks for the extras. :-)
--
Rob
Nov 4 '05 #9
VK
My bad!

Microsoft indeed has "onchange" handled supported for <checkbox>
elements despite it's not documented and not listed on MSDN:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/input_checkbox.asp>
But programmed state switch seems doesn't trig the event neither in IE
nor in FF. An attempt to sent such event "manually" leads to access
violation error in FF.

If we follow the old Netscape Gold scheme (the only one really we
should follow in forms :-) the OP problem can be solved very easy by:
....
<input type="checkbox" name="check001" value="checkbox"
onclick="someAction(this)">
....

and:
....
myCheckboxes[i].click();
....

If it has to be the 'change' event and nothing else, then the solution
(if it exists) is not known to me.

Nov 4 '05 #10
RobG wrote:
Thomas 'PointedEars' Lahn wrote:
RobG wrote:
it's in the HTML 4 specification.

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-INPUT>

The relevant specifications here are W3C DOM Level 2+ Events:

<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents>

<http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-eventgroupings-htmlevents>
You can go to the event spec and see what elements they apply to, or
look at the elements to see what attributes they have to find out what
events they support.
Not necessarily. It is conceivable that an event is handled by an object
without having the respective element an appropriate intrinsic event
handler attribute. One reason is a DOM that is ahead of markup in this
regard (of which examples exist in Valid HTML), another that the event
may bubble up to an object that handles the event and the element its
represented by has the required event handler attribute.
Six of one, half a dozen of the other to me, but thanks for the
extras. :-)


You're welcome :)
PointedEars
Nov 4 '05 #11
aundro wrote:
"VK" <sc**********@yahoo.com> writes:
Why not just do it in one step:

checkboxes[i].checked = true;
someOtherActionWith(checkboxes[i]);
Well, that's the way I'm proceeding right now, while I was waiting for
a possible better solution but, as nobody seems to be really inspired,
I guess there are none.

Also this post was, I admit, a bit oriented towards a more
general "Can someone tell/teach me about 'simulated' events?" ;)


If you are using intrinsic events (the onchange property of the INPUT
element or the corresponding onchange attribute in the HTML) rather than
the DOM addEventListener or IE attachEvent methods, and you do not use
(or need to use) the event objet itself in the event handlers, then you
probably don't need to worry about simulating events. You can just
explicitly call the INPUT element's onchange handler after you set the
checked property:-

checkbox.checked = true;
checkbox.onchange(); //<-- explicitly call the handler.

Or test the state of the checked property prior to assignment and only
call the handler if you are changing the property.

/* In comparison the left-hand side is evaluated first
(so before the assignment in the right hand side
expression):-
*/
if(checkboxe.checked != (checkbox.checked = ture)){
checkbox.onchange(); //<-- explicitly call the handler if changed
}
(I honestly though programmatically checking a checkbox would
trigger it's onchange handler)


The first thing that needs saying is that you should not expect an
onchange event to fire on a chackbox or radio button even if it is
checked/unchecked by the user. The specifications say that an onchange
event is fired when the _value_ of a form control changes, and setting a
radio/checkbox checked/unchecked does not alter the _value_ of that
control. Implementations vary, but if you try to use onchange with a
checkbox or radio control you are already in the area of unreliable
outcomes.

Generally no event handlers are fired when scripted actions trigger
behaviour that would have fired events if the user triggered them. So,
if you submit a form by calling the form's submit method the onsubmit
handler of the form is not called. This may seem unhelpful but it is
quite reasonable. The event handlers allow scripts to notice that events
happen and react to them. If the script itself is triggering the action
then it doesn't need to be told that the action is happening, it already
knows. And if calling the event handler is the most sensible reaction
then it is (or can be) in a position to do that. For example, if a
script wanted to submit a form conditionally upon the return value of
the form's onsubmit handler it can do:-

if(formRef.onsubmit()){ //<-- explicitly call the onsubmit handler
formRef.submit(); //<-- submit the form if it returned true.
}

- and if the onsubmit handler returns false the form will not be
submitted. Of course the onsubmit handler has to explicitly return true
if the form should be submitted, but the author of the code knows that
and so will ensure that it happens.

Richard.
Nov 4 '05 #12
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:
The first thing that needs saying is that you should not expect an
onchange event to fire on a chackbox or radio button even if it is
checked/unchecked by the user. The specifications say that an onchange
event is fired when the _value_ of a form control changes, and setting a
radio/checkbox checked/unchecked does not alter the _value_ of that
control. Implementations vary, but if you try to use onchange with a
checkbox or radio control you are already in the area of unreliable
outcomes.
Good to know (that is: to be remembered; as I must have read that some
time ago, but couldn't remember it).

Generally no event handlers are fired when scripted actions trigger
behaviour that would have fired events if the user triggered them. So,
if you submit a form by calling the form's submit method the onsubmit
handler of the form is not called. This may seem unhelpful but it is
quite reasonable. The event handlers allow scripts to notice that events
happen and react to them. If the script itself is triggering the action
then it doesn't need to be told that the action is happening, it already
knows.
Well, after all, that makes perfect sense ;)
Richard.


Thank you very much,

Arnaud
Nov 4 '05 #13
"VK" <sc**********@yahoo.com> writes:
As I said your question as posted was "How to trig an event from an
element if such element is not native EventProducer of the event in
question?".

Checkbox is not EventProducer of the "onchange" event (<select> is).
Theoretically you can create any artificial event using
document.createEvent (Gesko) and document.cretateEventObject (IE)
methods.
Yes, but I didn't feel like doing so. I don't need any event property
anyway, so there's no point in faking one.
But trying to dispatch artificial event to an element in
violation of its EventProducer model *seems* to lead to the security
exeption and script abort in FireFox.


Heh, I suppose mimicking the event model is at your own risks. (I
haven't read much about that in the case of browsers, but I know when
one has to use such practices, most of the time there's something
wrong in the whole design)

Thank you!

Arnaud
Nov 4 '05 #14

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

Similar topics

1
by: MonkeyBoy | last post by:
I am doing some some HTML manipulation in client-side script (IE5.x and IE6.x browsers only). Something like.. var tmpHTML = oTable.outerHTML // do something to the HTML her oTable.outerHTML =...
1
by: Adarsh Bhat | last post by:
Hi all, I understand that the onchange event occurs when the value of a form element changes (and after the element loses focus). But what about the case where the value of a text box (for...
4
by: Bart van Deenen | last post by:
Hi all I have a script where I dynamically create multiple inputs and selects from a script. The inputs and selects must have an associated onchange handler. I have the script working fine on...
1
by: Tom Leylan | last post by:
I'm either passing the wrong thing, comparing the wrong thing or... something else. I have a CSharp base class which is used to fire events and in CSharp I have defined a delegate for the custom...
2
by: uoziod | last post by:
Hello. Sorry for my english :) I got a next problem. This is a HTML. <ul> <li id="cat3"><input type="checkbox" onChange="javascript:groupCatCheck('3', this)" name="categories" value="3"...
4
by: parsifal | last post by:
Hi everyone :-) In my Page_Load, I create a control, and do an AddHandler on it (VB.NET). The button is created and displayed, and does cause a postback, but the event handler isn't fired. ...
12
by: Zytan | last post by:
I could call chkMyCheckBox_CheckedChanged() myself, but this is the response to the click, not the click itself. While this is fine for a button, it is insufficent for a check box, as the check...
3
by: HugeBob | last post by:
Hi All, I'm creating textareas via the DOM and I'm trying to add the onchange event to them. Here's my code: mytextarea = document.createElement("textarea"); mytextarea.onchange = mymethod;...
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...
0
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...
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,...

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.