Jul 25, 2008

Cannot Access Checkbox

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

Cannot Access Checkbox

darran
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
CODE
<input name="lead_role" id="lead_role" type="checkbox" class="style30" value="1">


And this is the validation I am testing out
CODE
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

Reply

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

HTML
<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.

 

 

 


Reply

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

CODE
<head>&lt;script language="javascript" type="text/javascript" src="validation.js"></script></head>


This is how my form was created.

CODE
<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.

Reply

saga
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

Reply

darran
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 smile.gif

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : access checkbox

  1. Make One Checkbox Checks All The Others - that's a tutorials and not a question (1)



Looking for access, checkbox

Searching Video's for access, checkbox
advertisement



Cannot Access Checkbox



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE