SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    alert(arr); // a,b,c,d,e,f,g,h

    Message box displays a,b,c,d
    I want, message box displays a,b,c,d,e,f,g,h
    While loop will be used.
    Code:
      
    <script type="text/javascript">
    
    window.onload = function (){
    var el = document.getElementsByTagName('select');
    var arr = [] ;
    var s, o,  i = 0, n = 0;
    while( s = el[i++]) { 
    while( o = s.options[n++] ){ arr.push(o.value); 
    }
    }
    alert(arr); // a,b,c,d
    }
    </script>
    
    <select>
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
    </select>
    
    <select>
    <option value="e">e</option>
    <option value="f">f</option>
    <option value="g">g</option>
    <option value="h">h</option>
    </select>

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think the problem is that you aren't resetting n to 0 after the first <select> iteration. You probably need to add something like this:
    Code:
    if (n === s.options.length - 1) n = 0;

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.onload = function (){
    	var selects = document.getElementsByTagName('select');
    	var arr = [] ;
    	for(var i = 0; i < selects.length; i++) {
    		var sel = selects[i];
    		for(var j = 0; j < sel.options.length; j++)
    			arr.push(sel.options[j].value); 
    	}
    	alert(arr);
    }

  4. #4
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply...
    Stereofrog,
    Your code did it.
    Result : While loop will not be used.


Bookmarks

Posting Permissions

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