...

View Full Version : Javascript array with only one item not an array?



Reveller
08-02-2006, 02:39 PM
Consider the code below: if I select "Promote off" from the dropdown menu, story_select alerts "103", as expected. But when I select "Comments on", story_select returns "undefined". The story_properties['comments']-array (which is read in the latter case) contains only one item: 103. Why, then isn't "103" returned in the second case? Is a javascript array with just one item not considered an array?


<script>
var story_properties = new Array();
story_properties['comments'] = new Array(103);
story_properties['promote'] = new Array(103, 106, 113);

function story_select(option) {
var form = document.forms[0];
var option = option.split('-'), property = option[0], value = option[1];
alert(story_properties[property][0]);
form.elements['ding'].selectedIndex = 0;
}
</script>

<form>
<select name="ding" onchange="story_select(this.options[this.selectedIndex].value)">
<option value="0">Kies hier...</option>
<option value="comments-on">Comments on</option>
<option value="promote-off">Promote off</option>
</select>
</form>

vwphillips
08-02-2006, 02:46 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<script>
var story_properties = new Array();
story_properties['comments'] = [103];
story_properties['promote'] = [103, 106, 113];

function story_select(option) {
var form = document.forms[0];
var option = option.split('-'), property = option[0], value = option[1];
alert(story_properties[property][0]);
form.elements['ding'].selectedIndex = 0;
}
</script>

<form>
<select name="ding" onchange="story_select(this.options[this.selectedIndex].value)">
<option value="0">Kies hier...</option>
<option value="comments-on">Comments on</option>
<option value="promote-off">Promote off</option>
</select>
</form>
</body>

</html>

new Array(103); creates an arry with 103 fields

Reveller
08-02-2006, 02:54 PM
Thanks! This problem has been bugging me for 2 hours, and I just overlooked the stupidity of it :)

Reveller
08-02-2006, 05:44 PM
I now have the following code, but it doesn't work for some reason. The objective is to loop through the checkboxes, and whenever the value of a checkbox is in the story_properties['comment'] array, it should be checked. No checkboxes are being checked, though. I have alerted several things (like boxes[i].value) and everything seems to be OK to my knowledge. What am I doing wrong?


var story_properties = new Array();
story_properties['comments'] = [103, 102, 100, 98];

function story_select(option) {
var form = document.forms[0], boxes = form.elements['edit[stories][]'];
var option = option.split('-'), property = option[0], value = option[1];
for (i = 0; i < boxes.length; i++) {
if (in_array(boxes[i].value, story_properties[property])) {
boxes[i].checked = true;
}
}
}

function in_array(needle, ar) {
for (var el in ar) {
if (ar[el] === needle) return el;
}
return false;
}

<form>
<input type="checkbox" name="edit[stories][]" value="100">First weblog post
<input type="checkbox" name="edit[stories][]" value="103">Second weblog post
<input type="checkbox" name="edit[stories][]" value="104">Etc...
<input type="checkbox" name="edit[stories][]" value="105">Etc...
<input type=button" onclick="story_select('comment')">
</form>

vwphillips
08-02-2006, 06:20 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
<!--
var story_properties = new Array();
story_properties['comments'] = [103, 102, 100, 98];

function story_select(option) {
var form = document.forms[0], boxes = form.elements['edit[stories][]'];
for (i = 0; i < boxes.length; i++) {
for (i1 = 0; i1 <story_properties[option].length; i1++) {
if (boxes[i].value==story_properties[option][i1]) {
boxes[i].checked = true;
}
}
}
}

function in_array(needle, ar) {
for (var el in ar) {
if (ar[el] === needle) return el;
}
return false;
}


//-->
</script><form>
<input type="checkbox" name="edit[stories][]" value="100">First weblog post
<input type="checkbox" name="edit[stories][]" value="103">Second weblog post
<input type="checkbox" name="edit[stories][]" value="104">Etc...
<input type="checkbox" name="edit[stories][]" value="105">Etc...
<input type="button" onclick="story_select('comments')" value="Tst">
</form>
</body>

</html>

Reveller
08-02-2006, 06:48 PM
Thanks Vic, your solution works perfectly! But could someone please explain to me why my solution (with in_array) doesn't work?

Pyth007
08-02-2006, 08:57 PM
The reason why yours didn't work is because of a few typos and a logical error of an operator. First the typos:

<input type="button" onclick="story_select('comments')">
This line was missing the opening " around button, rendering the rest of the page as a part of the quote. Also the onclick was calling story_select with the parameter of 'comment' instead of 'comments' which is the actual "array" index name. Also, your js was not surrounded by <script> tags.

As for the logical error... In in_array function, you have:

if (ar[el] === needle) return el;
which asks whether the object in ar[el] is identical to needle. When you were calling in_array, you were passing in the boxes.value; the value attribute (as well as all attributes in HTML) are strings. However, your array is a listing of numbers. Thus a number is not identical to a string, and so nothing would be checked. If, however, you change your conditional to check for [I]equality instead of identity (ie change "===" to "=="), then your function works as designed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum