onclick on checkbox

M

Michel Bany

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
 
R

RobG

Michel said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top