Connecting Tech Pros Worldwide Forums | Help | Site Map

Multiselection on simple click with HTML/JavaScript

Newbie
 
Join Date: Dec 2007
Posts: 3
#1: Dec 11 '07
Is there any way to multiselest the options without holding the ctrl or Shift key..
Thanks
prty

Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Dec 11 '07

re: Multiselection on simple click with HTML/JavaScript


Use a group of checkboxes instead of a select for this.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#3: Dec 11 '07

re: Multiselection on simple click with HTML/JavaScript


yes ... that would be a better way ... however you could use something like the following example:

[HTML]<html>
<head>
<script type="text/javascript">
var selected = {};

function select_option(opt) {
if (typeof selected[opt.value] == 'undefined') {
selected[opt.value] = 1;
} else {
delete selected[opt.value];
}

var s = opt.parentNode.getElementsByTagName('option');

for (var i = 0; i < s.length; i++) {
if (typeof selected[s[i].value] != 'undefined') {
s[i].selected = true;
} else {
s[i].selected = false;
}
}
}
</script>
</head>

<body>
<select multiple>
<option value="1" onclick="select_option(this);">1</option>
<option value="2" onclick="select_option(this);">2</option>
<option value="3" onclick="select_option(this);">3</option>
</select>
</body>
</html>
[/HTML]
kind regards
Newbie
 
Join Date: Dec 2007
Posts: 3
#4: Dec 12 '07

re: Multiselection on simple click with HTML/JavaScript


Quote:

Originally Posted by mrhoo

Use a group of checkboxes instead of a select for this.

Yeah..thats a good idea..
Thanks
Reply