470,604 Members | 2,020 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

onclick on checkbox

Hello everybody,
I am attaching an onclick event handler to a checkbox, using a <script> tag
<script>document.getElementById(id).onclick = ...</script>
and I have three questions.
1) In the handler I need to capture value of the "checked" property
My assumption is that I am getting the old value of this property, i.e.
before it has been toggled. And since I need the value that will result
from the click, I am coding this
// Compute the value that will result from the click
var checked = !document.getElementById(id).checked;
Is my assumption correct ?
2) I need to send the toggled value of the checked property to my
server using an AJAX so that I can modify some portions of
my document. After the AJAX is completed and document modified
I would expect that the checkbox view appears toggled, but it is not.
Any idea why ?
3) I tried to call the click() method in my onclick handler.
Is that legal ? Shouldn't this produce an infinite loop ?
Thanks,
Michel
Sep 27 '05 #1
1 2016
Michel Bany wrote:
Hello everybody,
I am attaching an onclick event handler to a checkbox, using a <script> tag
<script>document.getElementById(id).onclick = ...</script>
and I have three questions.
1) In the handler I need to capture value of the "checked" property
My assumption is that I am getting the old value of this property, i.e.
Try:

<input type="checkbox" onclick="alert('Checked? ' + this.checked);">

and you have your answer.
before it has been toggled. And since I need the value that will result
from the click, I am coding this
// Compute the value that will result from the click
var checked = !document.getElementById(id).checked;
Is my assumption correct ?
2) I need to send the toggled value of the checked property to my
server using an AJAX so that I can modify some portions of
my document. After the AJAX is completed and document modified
I would expect that the checkbox view appears toggled, but it is not.
Any idea why ?
Lots, but I'd be guessing about what your code does. URL? Code snippet?
3) I tried to call the click() method in my onclick handler.
Is that legal ? Shouldn't this produce an infinite loop ?


Does the element have a click() method? Which browser supports the
click method for that element?

<input type="checkbox" onclick="
alert('Do I support click()? ' + ((this.click)?'yes':'no'));">

Supposing it does, why do you want to do that? If you want to check it,
then set its checked attribute to true. :-)
--
Rob
Sep 27 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by F. Da Costa | last post: by
6 posts views Thread by Edward | last post: by
2 posts views Thread by Nemo | last post: by
7 posts views Thread by Ryan Ternier | last post: by
7 posts views Thread by RHPT | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.