Marking an element as .checked and reading innerHTML

S

sicapitan

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?
 
M

Martin Honnen

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)
 
S

sicapitan

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?
 
S

sicapitan

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
 
S

sicapitan

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top