/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Multiple Select Drop Down

In the following script I can click one item,
use shift to click a range of items
and ctrl-click to select discontinuous items.

I know I can use the ‘selectedIndex’ to get a single selection.

How do I get an array of the multiple items I select?
That what I really want to return instead of the alert message.

Any ideas? ?

[code=php]
<html>
<head>
<title>Multiple Selection Drop Down</title>
<script type=”text/javascript”>
function SetMDDsize(mddSize) {
document.getElementById(‘MultDD’).size = mddSize;
}
function GetMDDselections() {
document.getElementById(‘MultDD’).size = 1; // reduce screen size
alert(‘Display selected options here – How???’); // display all checked option here.
document.getElementById(‘MultDD’).selectedIndex = -1; // reset list
// return string of items selected separated by ‘,’ characters
}
</script>
</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″>0</option>
<option value=”1″>1</option>
<option value=”2″>2</option>
<option value=”3″>3</option>
<option value=”4″>4</option>
<option value=”5″>5</option>
<option value=”6″>6</option>
<option value=”7″>7</option>
<option value=”8″>8</option>
<option value=”9″>9</option>
</select>
<button id=”MultDDbtn” onClick=”GetMDDselections()”>Get Selections</button>
</body>
</html>
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERauthorNov 08.2007 — I've played with it some more and came up with this.

Seems to work. Anyone see any problems with it? ?

[code=php]
<html>
<head>
<title>Multiple Selection Drop Down</title>
<script type="text/javascript">
function SetMDDsize(mddSize) {
document.getElementById('MultDD').size = mddSize;
}
function GetMDDselections() {
document.getElementById('MultDD').size = 1; // reduce screen size
var picked = '';
for (i=0; i<document.getElementById('MultDD').options.length; i++) {
if (document.getElementById('MultDD').options[i].selected == true) { picked += i+','; }
}
// alert('Selected: '+picked);
// alert('Number of Options: '+document.getElementById('MultDD').options.length);
// alert('Display selected options here - How???'); // display all checked option here.
document.getElementById('MultDD').selectedIndex = -1; // reset list
return picked;
}
</script>
</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">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<button id="MultDDbtn" onClick="alert(GetMDDselections())">Get Selections</button>
</body>
</html>
[/code]
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...