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

Cannot Access Elements of set innerHTML Forms

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

Oct 5 '05 #1
Share this Question
Share on Google+
5 Replies


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

Oct 5 '05 #2

P: n/a
sy************@gmail.com 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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 5 '05 #3

P: n/a
sy************@gmail.com 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
--
remove blah to email me
Oct 5 '05 #4

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

Oct 6 '05 #5

P: n/a
va*****@gmail.com wrote:
[...]

innerHTML is considered "deprecated"


I think a more accurate characterisation would be "never to be
standardised". ;-).

[...]

--
Rob
Oct 6 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.