Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Javascript array with only one item not an array?

    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?

    Code:
    <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>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    Code:
    <!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
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! This problem has been bugging me for 2 hours, and I just overlooked the stupidity of it

  • #4
    New Coder
    Join Date
    Aug 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Code:
    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>

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    Code:
    <!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>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #6
    New Coder
    Join Date
    Aug 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Vic, your solution works perfectly! But could someone please explain to me why my solution (with in_array) doesn't work?

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The reason why yours didn't work is because of a few typos and a logical error of an operator. First the typos:
    Code:
    <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:
    Code:
    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[i].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 equality instead of identity (ie change "===" to "=="), then your function works as designed.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •