www.webdeveloper.com
Results 1 to 2 of 2

Thread: store only selected multi options

  1. #1
    Join Date
    Dec 2012
    Posts
    4

    store only selected multi options

    Using following code to view two column chooser. Issue is loop below , I get all from the list ( selected, unselected )

    How is it possible to get only the selected.

    Code:
    				<div class="selectTask">
    					<form name="TaskList" method="post" action="TaskList.asp">
    						<select name="searchable[]" id='searchable' multiple='multiple'>
    							<?php foreach( $output as $each ): ?>
    							<?php $var++?>
    							<option value="$var"><?=$each?></option>
    							<?php endforeach; ?>
    						</select>
    					</form>
    				</div>
    				<div class="clickButtons">
    					<input type = "button"
    					 value = "clear selection" />
    
    					<input type = "button"
    					onClick="send_message()"
    					value = "remove top task and send message to next" />
    				</div>
    Code:
    		function send_message(){
    			var selectedArray = new Array();
    			var selObj = document.getElementById('searchable');
    			var i;
    			var count = 0;
    			for (i=0;i<selObj.options.length;i++)
    			{
    				if (selObj.options[i].selected)
    				{
    					selectedArray[count] = selObj.options[i].text;
    					count++;
    				}
    			}
    			alert(selectedArray);
    		}
    	</script>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Lightbulb

    Cannot test without your PHP data, but the following works as standalone.
    Compare and contrast to your version.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    Click, Shift-Click or Ctrl-Click<br />
    <select multiple id="MultDD"> <!-- size="1" onClick="SetMDDsize(10)" -->
    <option value="">-Select-</option>
    <option value="0">Zero</option>  <option value="1">One</option>
    <option value="2">Two</option>   <option value="3">Three</option>
    <option value="4">Four</option>  <option value="5">Five</option>
    <option value="6">Six</option>   <option value="7">Seven</option>
    <option value="8">Eight</option> <option value="9">Nine</option>
    </select>
    <button id="MultDDbtn" onClick="alert(getSBoxPicks('MultDD'))">Get Selections</button>
    
    <script type="text/javascript">
    function getSBoxPicks(ids) {			// used with 'multiple' option of <select> tag
    //  document.getElementById(ids).size = 1;	// reduce screen size
      var tarr = [];
      for (i=0; i<document.getElementById(ids).options.length; i++) {
    	if (document.getElementById(ids).options[i].selected == true) { tarr.push(i); }
      } 
    //  document.getElementById(ids).selectedIndex = -1;		// reset list
      return tarr.join();
    }
    </script>
    
    </body>
    </html>
    If problem remains, check the info being collected by PHP section.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles