473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Cannot Access Elements of set innerHTML Forms

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
5 4055
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jon Davis | last post by:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current browsers using: <object id="extendedhtml" type="text/html" data="otherpage.html" width="250" height="400"></object> ...
11
by: Jon Hoowes | last post by:
Hi, I have inherited some code that has some form elements (radio buttons) that are called "1", "2" etc. for example: <input name="2" type="radio" value="45"> <input name="2" type="radio"...
2
by: Ed | last post by:
Hi, I'm stuck at the moment trying to work out how to access the value of a textfield which gets added in a DIV element. I have functions to add and remove the textfields from the elements as...
2
by: Greg Linwood | last post by:
How does one access HTML elements from code-behind? I've got a mix of asp:Labels & <font> elements - I'd like to continue using <font> elements, but be able to access them from code-behind to set...
3
by: robert.oschler | last post by:
I have an AJAX driven page where I dynamically add rows to a known table on the page, based on the return document from the AJAX call, using DOM node methods (except for a small snippet of code,...
5
by: theseer | last post by:
<input type='text' name='foo1bar' /> <input type='text' name='foo2bar' /> If I wanted to set a value for both of these in a for loop how would I go about referencing these elements with a...
0
by: jbonifacejr | last post by:
Hello everyone. Very sorry if I am posting in the wrong place. I am writing a test harnes that will be required to interact with elements on a web page, such as inserting text in a text box. At...
4
by: Harry | last post by:
Hi Guys I dont really know how to do this: if there are a page of others, some data are embedded inside the data array like this: <script language="JavaScript" type="text/javascript"> <!-- var...
4
by: omlac | last post by:
im creating controls using javascript, and i would like to retrieve the values of the controls in code behind(vb.net/c#) . My vb.net below is not returning them, Odata is a div but i added the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.