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
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Excessive Looping

    Why does this loop more than the specified amount of iterations? I have a feeling that I'm overlooking a minor type or the like, but it is really starting to bug me that I can't find it. value is equal to 2.

    Code:
    function addMe(e){
      var list = document.daForm.listItems; // var list references the <select> for this function
      var value = list.options[e].value;
      var underS = '';
    
      for(i = 0; i < (value + 2); i++){ // adds the right # of underscores to child
        underS += '_';
        alert(underS);
      }
    Last edited by Antoniohawk; 04-25-2004 at 04:11 AM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Simple! The type of the value property of form controls is string. Thus, what you are doing is that you're adding a numerical 2 to the string '2', thus generating the string '22'. That string is then autocast into a number by the < operator, thus resulting in numerical 22.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Lol, I knew that it was something simple. Ahh the wonders of being a javascript newbie Another question while I'm at it. If I select more than one thing in the select field, how can I tell what has been selected? Also, is it possible to somehow enter in 2 spaces before an item in the select box so that it is indented. I have attempted to use ' ' and nbsp; but to no avail.
    Last edited by Antoniohawk; 04-26-2004 at 02:30 AM.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    If the type of combobox is set as multiple, you have to loop through the option items and check the selected property to see if it's true or false.
    Code:
    for (var i=0;i<refToCombo.options.length;i++){
      if (refToCombo.options[i].selected) {
          //item i is selected
      }
    }
    In your second question, if you mean adding 2 spaces to the option item programmatically, then try using \u00a0\u00a0

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Any way to do it unprogrammatically[even a word?]?
    Last edited by Antoniohawk; 04-26-2004 at 10:50 PM. Reason: misspelled word

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    using &nsbp;
    in select do make a space.

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You're right! I wonder what I was trying before. I probably forgot the '&'. Thank you Garadon.


  •  

    Posting Permissions

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