aspose file tools*
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
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Unable to retrieve Checkbox state" Watch "Unable to retrieve Checkbox state" New topic
Author

Unable to retrieve Checkbox state

Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
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
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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: http://pascarello.brinkster.net/examples/testNewForm.html and you can see how to get the name to added to the element.

Eric
Srikkanth Mohanasundaram
Ranch Hand

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

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


I should have come here 2 days back

Regards,
Srikkanth.M
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unable to retrieve Checkbox state