Solved

Undefined checkbox

Posted on 2004-09-05
3
1,273 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now