Cannot Access Elements of set innerHTML Forms

S

syntheticninja

I am trying to loop through a form to get all the check boxes that
exist on that particular form. My problem is the checkboxes are
dynamically added to the page after it has loaded (using the innerHTML
of a div).

When I do this, I get back that there are no elements on the form.

This is how I try to loop through the form.

function getChecked(oForm) {
var el, i = 0;
while (el = oForm.elements[i++]) if (el.type == 'checkbox') //do stuff
}

If I access the information included with the innerHTML by itself, it
works fine. Ont the page where it gets placed, it cannot be found.
 
C

Cigol

I can also access the elements directly, if I knew them ahead of time
using getElementById(element).

Only problem is I could only use that for testing as I don't lknow when
the checkbox will exist.
 
R

Randy Webb

(e-mail address removed) said the following on 10/5/2005 9:20 AM:
I am trying to loop through a form to get all the check boxes that
exist on that particular form. My problem is the checkboxes are
dynamically added to the page after it has loaded (using the innerHTML
of a div).

Use .createElement instead of innerHTML. You are seeing one of the
problems of innerHTML.
 
G

Gérard Talbot

(e-mail address removed) a écrit :
I am trying to loop through a form to get all the check boxes that
exist on that particular form. My problem is the checkboxes are
dynamically added to the page after it has loaded (using the innerHTML
of a div).

When I do this, I get back that there are no elements on the form.

This is how I try to loop through the form.

function getChecked(oForm) {
var el, i = 0;
while (el = oForm.elements[i++])

assignment are resolved eventually as a boolean condition but they
create side effects which you should know very well. Otherwise, you
should prefer other ways to go through the list of form controls.

if (el.type == 'checkbox') //do stuff
}

If I access the information included with the innerHTML by itself, it
works fine.

That too is not recommendable IMO. You should use DOM methods instead:
future proof and reliable.

Gérard
 
V

vallini

If you are doing something like this:

========================================
========================================
<body bgcolor="#ffffff" text="#000000">
<form>
<div id="foo"></div>
</form>

<script language="JavaScript"><!--
document.getElementById("foo").innerHTML='<input type="checkbox"
value="1"><input type="checkbox" value="2"><input type="checkbox"
value="3"><input type="checkbox" value="4">';

function getChecked(oForm) {
var el, i = 0;
while (el = oForm.elements[i++]) if (el.type ==
'checkbox')alert(el.value);
}
getChecked(document.forms[0])
//--></script>
</body>
========================================
========================================

That works fine on both my IE6 and Firefox 1.0.7
So I an't see where your problem comes from. perhaps a more complete
example would help.

innerHTML is considered "deprecated" (comparatively, one of the most
controversial decisions ever). Yet no gen 5 browser I know of has
dropped support for it or palns to drop it, likewise they do not plan
to drop support for FONT.
I personally go on using it as one of the fastest ways to read the html
contents of a layer, and also one of the most convenient ways to write
onto it not too complex html formattings.

ciao
Alberto
http://www.unitedscripters.com/
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top