Solved

Undefined checkbox

Posted on 2004-09-05
3
1,277 Views
Last Modified: 2012-05-05
The program will return an undefined string when there is only one checkbox. Why and how to fix it?

thanks.
0
Comment
Question by:hawkly
3 Comments
 
LVL 14

Accepted Solution

by:
ziffgone earned 25 total points
ID: 11986920
Pretty vague question, but try this out:

<script type="text/javascript">
var chkbox= document.form_name.elements['checkbox_name'];
if(chkbox.checked == true){
       var val = document.formname.elements['checkbox_name'].value;
}
else{
       var val = "";
}
alert(val);
</script>

Hope that helps.

Regards...
0
 
LVL 7

Expert Comment

by:Xxavier
ID: 11987020
"The program will return an undefined string when there is only one checkbox, Why and how to fix it?"

Probably because when there is only one checkbox the string variable is not assigned a value. To fix it make sure the variable is assigned a value when there is only one checkbox
0
 
LVL 8

Assisted Solution

by:RozanaZ
RozanaZ earned 25 total points
ID: 11987413
<HTML>
<HEAD>
<TITLE></TITLE>

<script>
function getValue()
{
      checkObj = document.getElementById("ch1");
      alert("Selected: " + checkObj.checked + " value: " + checkObj.value);
}
</script>
</HEAD>
<BODY>
<form>
<input type="checkbox" name="ch1" id="ch1" value="CheckBox 1">CheckBox 1<br>
<input type="button" value="Get Value" onClick="getValue()">
</form>
</BODY>
</HTML>
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question