By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

onclick on checkbox

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.