This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Unable to retrieve Checkbox state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Unable to retrieve Checkbox state" Watch "Unable to retrieve Checkbox state" New topic

Unable to retrieve Checkbox state

Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 237
Hi all,

I'm adding a row dynamically using JS.That row will contain a checkbox and hyperlink.
I'm able to get this done.But when i do a document.forms[0].checkboxname.checked i'm getting an error,It says it's undefined.

with this code i'm able to add a row.

But I also want to use the checkbox, is it possible to use it?

I'm doing a document.forms[0].chk it says it's undefined..

Is it not possible to use a dynamically added checkbox,I thought the DOM tree would in the browser's memory and it's possible to use the checkbox.
[ August 09, 2007: Message edited by: Srikkanth Mohanasundaram ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

I'm not sure why it's not working for you, but before you spend a whole lot more time on this, be aware that using refereces like document.forms[0] is very old-fashioned and error prone. (What if a new form gets added to the page before this form? Your code is broken!)

What I tend to do is to assign id values to elements that I need to reference and use document.getElementById() to directly and unequivically reference the element.

[Asking smart questions] [About Bear] [Books by Bear]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
The reason it is not working is because the browser in use is IE and it does not assign the name attribute when you use DOM methods.

Do a view source on this page: and you can see how to get the name to added to the element.

Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 237
Thanks that was very useful,I was breaking my head over this issue for 2 days.

That was a good suggestion,but i'm not supposed tweak any working code

I should have come here 2 days back

I agree. Here's the link:
subject: Unable to retrieve Checkbox state
jQuery in Action, 3rd edition