Solved

Undefined checkbox

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

717 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