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

Marking an element as .checked and reading innerHTML

P: n/a
I have this code snippet:

updateProps snippet:

if (mycheckbox.checked == '1')
? $('mycheckbox').checked = true
: $('mycheckbox').checked = false;

content = $('mydiv').innerHTML;

html snippet:

<div id="mydiv">
<input id="mycheckbox" class="checkbox" type="checkbox" value=""
tabindex="14" onchange="(this.checked) ? checkVal = '1' : checkVal =
'0';updateProps(checkVal, 'mycheckbox')" />
</div>

The problem is that when I display the contents of content it doesnt
have 'checked'. But if I alert($('mycheckbox').checked); it says it is
true

Is this a problem with innerHTML not grabbing the correct data or the
checkbox not actually being checked?

Aug 26 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


sicapitan wrote:

if (mycheckbox.checked == '1')
checked is boolean property (assuming that is the checked property of an
input element object in the DOM), no idea why you want to compare that
to a string with one character '1', you probably simply want
if (mycheckbox.checked)
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 26 '06 #2

P: n/a
Sorry, i had :

(mycheckbox.checked == '1')
? $('mycheckbox').checked = true
: $('mycheckbox').checked = false;

if would throw an error

Either way its reading it fine, and writing it fine. well according to
my alerts. in otherwords, if on load i alert($('mycheckbox').checked) i
get 0

when i check the box, and write the variable to a JSON db, then i check
the checkbox its true. then when i go to grab the innerhtml of my div,
it doesnt come through as checked. it sounds like a problem with
innerhtml, but i dont know what could be causing it. im checking the
box, and then grabbing the contents and its not coming through as
checked.

any ideas?

Martin Honnen wrote:
sicapitan wrote:

if (mycheckbox.checked == '1')

checked is boolean property (assuming that is the checked property of an
input element object in the DOM), no idea why you want to compare that
to a string with one character '1', you probably simply want
if (mycheckbox.checked)
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 26 '06 #3

P: n/a
I think its because actuallly marking the checkbox as
mycheckbox.checked = true; acually change the html code, just its
state, so when I get innerHtml its still the old value, thats why. I
would need to update the div with the new contents of a checked
checkbox which would then be able to be read with .innerHTML

sicapitan wrote:
Sorry, i had :

(mycheckbox.checked == '1')
? $('mycheckbox').checked = true
: $('mycheckbox').checked = false;

if would throw an error

Either way its reading it fine, and writing it fine. well according to
my alerts. in otherwords, if on load i alert($('mycheckbox').checked) i
get 0

when i check the box, and write the variable to a JSON db, then i check
the checkbox its true. then when i go to grab the innerhtml of my div,
it doesnt come through as checked. it sounds like a problem with
innerhtml, but i dont know what could be causing it. im checking the
box, and then grabbing the contents and its not coming through as
checked.

any ideas?

Martin Honnen wrote:
sicapitan wrote:

if (mycheckbox.checked == '1')
checked is boolean property (assuming that is the checked property of an
input element object in the DOM), no idea why you want to compare that
to a string with one character '1', you probably simply want
if (mycheckbox.checked)
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 26 '06 #4

P: n/a
ok heres the rundown

If i have this as my html

<div id="mystuff">
<div id="innerstuff">
<input type="checkbox" value="" />
</div>
more stuff
</div>

and I have some javascript:

newstuff = "<input type=\"checkbox\" value =\"\" checked=\"checked\"
/>";
Element.update("innerstuff", newstuff);

alert($('innerstuff').innerHTML);

I get the change with checked="checked"

but if I

alert($('mystuff').innerHTML);

It does not show me the changed information within the innerstuff div.

is this normal?

Bearing in mind I dont want to rebuild the whole div, I just want to
update that field as checked, then grab it entirely with that change,
then load that html into a properties box

sicapitan wrote:
I think its because actuallly marking the checkbox as
mycheckbox.checked = true; acually change the html code, just its
state, so when I get innerHtml its still the old value, thats why. I
would need to update the div with the new contents of a checked
checkbox which would then be able to be read with .innerHTML

sicapitan wrote:
Sorry, i had :

(mycheckbox.checked == '1')
? $('mycheckbox').checked = true
: $('mycheckbox').checked = false;

if would throw an error

Either way its reading it fine, and writing it fine. well according to
my alerts. in otherwords, if on load i alert($('mycheckbox').checked) i
get 0

when i check the box, and write the variable to a JSON db, then i check
the checkbox its true. then when i go to grab the innerhtml of my div,
it doesnt come through as checked. it sounds like a problem with
innerhtml, but i dont know what could be causing it. im checking the
box, and then grabbing the contents and its not coming through as
checked.

any ideas?

Martin Honnen wrote:
sicapitan wrote:
>
>
if (mycheckbox.checked == '1')
>
checked is boolean property (assuming that is the checked property of an
input element object in the DOM), no idea why you want to compare that
to a string with one character '1', you probably simply want
if (mycheckbox.checked)
>
>
--
>
Martin Honnen
http://JavaScript.FAQTs.com/
Aug 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.