/    Sign up×
Community /Pin to ProfileBookmark

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][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>[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJun 12.2014 — Cannot test without your PHP data, but the following works as standalone.

Compare and contrast to your version.

<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8" /&gt;

&lt;title&gt; HTML5 page &lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;
Click, Shift-Click or Ctrl-Click&lt;br /&gt;
&lt;select multiple id="MultDD"&gt; &lt;!-- size="1" onClick="SetMDDsize(10)" --&gt;
&lt;option value=""&gt;-Select-&lt;/option&gt;
&lt;option value="0"&gt;Zero&lt;/option&gt; &lt;option value="1"&gt;One&lt;/option&gt;
&lt;option value="2"&gt;Two&lt;/option&gt; &lt;option value="3"&gt;Three&lt;/option&gt;
&lt;option value="4"&gt;Four&lt;/option&gt; &lt;option value="5"&gt;Five&lt;/option&gt;
&lt;option value="6"&gt;Six&lt;/option&gt; &lt;option value="7"&gt;Seven&lt;/option&gt;
&lt;option value="8"&gt;Eight&lt;/option&gt; &lt;option value="9"&gt;Nine&lt;/option&gt;
&lt;/select&gt;
&lt;button id="MultDDbtn" onClick="alert(getSBoxPicks('MultDD'))"&gt;Get Selections&lt;/button&gt;

&lt;script type="text/javascript"&gt;
function getSBoxPicks(ids) { // used with 'multiple' option of &lt;select&gt; tag
// document.getElementById(ids).size = 1; // reduce screen size
var tarr = [];
for (i=0; i&lt;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();
}
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;


If problem remains, check the info being collected by PHP section.
×

Success!

Help @singhai_nish spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...