Jump to content
xisto Community
Sign in to follow this  
darran

Cannot Access Checkbox

Recommended Posts

I did a form, I created a checkbox under the same name 'lead_role' but I can't seem to access it in my javascript validation.

This is the code in my HTML page

<input name="lead_role" id="lead_role" type="checkbox" class="style30" value="1">

And this is the validation I am testing out
alert(document.form1.lead_role.checked);

My browser returns me an 'undefined' alert message.

The only way I was successful in accessing the element is using the getElementById() method but that only gets the very first instance of the checkbox.

Can anyone help me out? Thanks
Edited by darran (see edit history)

Share this post


Link to post
Share on other sites

I made this code and tested it out in my browser, Firefox:

 

<head></head>
 <body> <form name="form1"> <input name="lead_role" type="checkbox" class="style30" checked> </form>

  <script> if (document.form1.lead_role.checked==1) { alert(document.form1.lead_role.checked); } </script></body>

 

I had to replace value="1" with checked because the latter worked at checking the box but the former didn't.

 

My guesses would be:

You tried to refer to an object that hadn't been already set (put the javascript after the html).

 

You didn't put the <form name="form1"> bit in your code, so when it looked for an object whose name was form1 it couldn't find it.

Anyway, if that doesn't solve the problem, it helps if you post the entire page of code that you are using.

Share this post


Link to post
Share on other sites

I don't think it is a problem you are saying. This is where my javascript is located.

<head><script language="javascript" type="text/javascript" src="validation.js"></script></head>

This is how my form was created.

<form name="form1" id="form1" onsubmit="return validate();" action="register_process.asp">

Please reply me ASAP as I am rushing for time.

I have attached the form as well as the javascript, hope to get a reply soon.

form.txt

js.txt

Share this post


Link to post
Share on other sites

You can not use or it will return an error if you try to use a form element or any object that belongs to the body section of an html until it is instantiated unless you load the code on the opening <body> tag using the onload="" event.So if you want to set the value of the checkbox do so by using the event onload="" in the <body> tag.By the way the attribute name="" is not supported by XHTML 1.0

Share this post


Link to post
Share on other sites

I have found a solution to the problem, but I don't know if it was actually the solution. I used the same name for every checkbox meaning 5 checkbox being named as lead_role or something. I name it as different seperate names, and it works perfectly fine. Thanks for all your help anyways :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.