Solved

Undefined checkbox

Posted on 2004-09-05
3
1,282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

737 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