Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,629 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,356 people online right now. Registration is fast and FREE... Join Now!




Getting an 'undefined' error.

 
Reply to this topicStart new topic

Getting an 'undefined' error.

AssassiNBG
post 11 Oct, 2008 - 11:44 AM
Post #1


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 4

Hey everyone! I have to say I'm new to the AJAX world and I'm stuck with what I think is a basic script. Disregarding function names (I'm modifying an example function from a tutorial), could you please help me out with this simple script:
CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <meta name="author" content="Vesselin">

    <title>testing javascript checkboxes</title>

<script type="text/javascript">
var cb = new Array();

function CheckAll(chk) {
    var j = 0;
for (i = 0; i < chk.length; i++) if (chk[i].checked) {cb[j] = chk[i].value; j++; document.getElementById('wtf').innerHTML = cb[j]; }
}

function UnCheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = false;
}

</script>    
    
</head>

<body>
<div id="wtf"></div>
<form name="myform" action="checkboxes.asp" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list" value="1">ASP<br>
<input type="checkbox" name="check_list" value="2">PHP<br>
<input type="checkbox" name="check_list" value="3">JavaScript<br>
<input type="checkbox" name="check_list" value="4">HTML<br>
<input type="checkbox" name="check_list" value="5">MySQL<br>

<input type="button" name="Check_All" value="Check All"
onClick="CheckAll(document.myform.check_list)">
<input type="button" name="Un_CheckAll" value="Uncheck All"
onClick="UnCheckAll(document.myform.check_list)">

</body>
</html>


I'm trying to end up with the array cb containing the values of the checked checkboxes but all I come up is an 'undefined' message.

Any help is very appreciated!
User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 11 Oct, 2008 - 11:58 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,976



Thanked 77 times

Dream Kudos: 1175
My Contributions


Here is a simple script to get the values of the checked checkboxes and add them into an array.

CODE
<script>
var arr = new Array();
function fillArray(){
  var str = "VALUES:{";
  var cb = new Array();
  var tags = document.getElementsByTagName("input");
  for(var i=0; i<tags.length; i++){
    if(tags[i].checked && tags[i].type == "checkbox"){
      cb[cb.length] = tags[i].value;
      if(cb.length > 1){
        str += ", "+tags[i].value;
      }
      else{
        str += ""+tags[i].value;
      }
    }
  }
  alert(str+"}");
  return cb;
}
</script>

<input type="checkbox" value="test">
<input type="checkbox" value="Able">

<input type="button" value="BTN" onclick="arr=fillArray();" />


Here it is when we get rid of the string alerting:

CODE
<script>
var arr = new Array();
function fillArray(){
  var cb = new Array();
  var tags = document.getElementsByTagName("input");
  for(var i=0; i<tags.length; i++){
    if(tags[i].checked && tags[i].type == "checkbox"){
      cb[cb.length] = tags[i].value;
    }
  }
  return cb;
}
</script>

<input type="checkbox" value="test">
<input type="checkbox" value="Able">

<input type="button" value="BTN" onclick="arr=fillArray();" />


NOTICE - The function creates a new array and returns that array, you have to set the returned array to an array otherwise it is lost.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

AssassiNBG
post 11 Oct, 2008 - 12:57 PM
Post #3


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 4

Hey BetaWar,

Thanks for the functions.

It seems that I don't know how to use the returned array called 'arr'. I seem to be getting an undefined error if I try to use it in the page body like this:

CODE

alert(arr[0]);
or
document.write(arr[0]);


Thanks again for the functions. But it looks like I still haven't gotten the grasp of javascript as a language so bare with me please.

Regards

This post has been edited by AssassiNBG: 11 Oct, 2008 - 12:58 PM
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 11 Oct, 2008 - 01:27 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,976



Thanked 77 times

Dream Kudos: 1175
My Contributions


No problem, could I see the code you are currently using (assuming that it is not the same as what you posted first)?

One thing to make sure of is that you have used the fillArray() function before you try to use it. That may be the reason you are getting undefined.

Try something like so where you are trying to use the array:
CODE

if(arr.length > 0){ // if there are items in the array
alert(arr[0]); // alert the first one
}
else{ // otherwise
alert("Array empty."); // alert "Array empty."
}


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

AssassiNBG
post 14 Oct, 2008 - 05:49 AM
Post #5


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 4

Hi!

Yeah, I figured it out and got it going. Thanks so much for the function again! smile.gif

Regards
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 05:29AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month