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

Testing checkbox in a javascript.

P: n/a
This has to be easy, but I'm at a loss.

In an externally linked js, I create a checkbox using innerHTML. How do I
test the value or test to see if it's checked while still within the js?

--
Ed Jay (remove M to respond by email)
Jan 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

P: n/a
Danny <da*******@bluebottle.com> wrote:
Check at http://www.javascriptkit.com/javatut...diocheck.shtml .

Thanks. I just finished solving the problem. Using js, I dynamically
create a checkbox in a display-toggled (display:none/"") container and
then detect the state of the checkbox. It didn't play when I create the
checkbox using innerHTML, but it works with the checkbox permanently
written to the document in the toggled container.

--
Ed Jay (remove M to respond by email)
Jan 15 '06 #3

P: n/a
Ed Jay wrote:
This has to be easy, but I'm at a loss.

In an externally linked js, I create a checkbox using innerHTML. How do I
test the value or test to see if it's checked while still within the js?


Get a reference to the checkbox and determine the value of its 'checked'
attribute:
<form action="">
<div id="divA">
</div>
</form>

<script type="text/javascript">
function insertCheckbox()
{
var h = '<input type="checkbox" name="check01" checked>';
document.getElementById('divA').innerHTML = h;
alert(document.forms[0].check01.checked);
}
insertCheckbox();
</script>
It worked OK in Safari and Firefox, but I would not guarantee it to work in
all browsers.

--
Rob
Jan 15 '06 #4

P: n/a
RobG said the following on 1/15/2006 12:44 AM:
Ed Jay wrote:
This has to be easy, but I'm at a loss.

In an externally linked js, I create a checkbox using innerHTML. How do I
test the value or test to see if it's checked while still within the js?

Get a reference to the checkbox and determine the value of its 'checked'
attribute:
<form action="">
<div id="divA">
</div>
</form>

<script type="text/javascript">
function insertCheckbox()
{
var h = '<input type="checkbox" name="check01" checked>';
document.getElementById('divA').innerHTML = h;
alert(document.forms[0].check01.checked);
}
insertCheckbox();
</script>
It worked OK in Safari and Firefox, but I would not guarantee it to work
in all browsers.


Firefox, Opera 7/8/9 and IE6 on WinXP all give a true alert so you can
say it works there :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 15 '06 #5

P: n/a
RobG <rg***@iinet.net.au> wrote:
Ed Jay wrote:
This has to be easy, but I'm at a loss.

In an externally linked js, I create a checkbox using innerHTML. How do I
test the value or test to see if it's checked while still within the js?


Get a reference to the checkbox and determine the value of its 'checked'
attribute:
<form action="">
<div id="divA">
</div>
</form>

<script type="text/javascript">
function insertCheckbox()
{
var h = '<input type="checkbox" name="check01" checked>';
document.getElementById('divA').innerHTML = h;
alert(document.forms[0].check01.checked);
}
insertCheckbox();
</script>
It worked OK in Safari and Firefox, but I would not guarantee it to work in
all browsers.


It works fine in Opera 8.5, too. That's essentially what I had, except I'm
not triggering an alert, I'm triggering a conditional statement. Unless
the element named 'check01' already exists, it can't be checked for state,
and the script fails. I coded the button into the document in a
display:none/"" container.

Triggering an alert doesn't tell the story, at least in this case. :-)

--
Ed Jay (remove M to respond by email)
Jan 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.