PDA

View Full Version : select multiple items without CTRL


mattmill30
06-27-2009, 11:32 AM
Hi,

Does anyone know how to get a <select multiple="true"> to allow the user to select multiple items, without the need of holding the CTRL key?

Thanks,

Matthew Millar

mattmill30
06-27-2009, 12:13 PM
Hi

Does anyone know how to get a <select multiple="true"> to allow the user to select multiple items, without the need of holding the CTRL key?

I've posted this in the xhtml section. but i've got a feeling its only doable using the onmousehover and then using a piece of javascript that simulates the CTRL key being held.

Does anyone know how i'd accomplish this?

I have no javascript experience, hence why i was hoping it was possible to do it via HTML.

Thanks,

Matthew Millar

coothead
06-27-2009, 12:35 PM
Hi there mattmill30,

to the best of my knowledge, it is not possible. :disagree:

mattmill30
06-27-2009, 08:08 PM
Thanks coothead.

While i was waiting for the reply i googled the problem and found this:

http://www.webdeveloper.com/forum/archive/index.php/t-63272.html

BigMoosie seems to say thats its possible to use the onkeydown attribute capture a key event and send another/replacement key event.

Could that perhaps be adapted to the onmousehover to do the second part, i.e. holding the CTRL key perhaps?

coothead
06-28-2009, 06:10 AM
Hi there mattmill30,

I have come up with a possible solution, but as IE does not recognise any
javascript events on the option element, it's use may not be really useful. :agree:

Also the code works onmouseover of the option elements but not onclick.

Still you may find it mildly interesting. :lol:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title></title>

<style type="text/css">
h4,#info {
text-align:center;
}
span {
color:#600;
}
form {
width:400px;
margin:0 auto 20px;
}
select {
width:100px;
background-color:#eef;
}
</style>

<script type="text/javascript">

function multipleTest() {

opt=document.forms[0].getElementsByTagName('option');
obj=document.getElementById('info').firstChild;

for(c=0;c<opt.length;c++) {
opt[c].onmouseover=function() {
this.selected=true;
obj.nodeValue+=this.value+', ';
}
}

document.forms[0][2].onclick=function(){
for(c=0;c<opt.length;c++) {
opt[c].selected=false;
}
obj.nodeValue='';
}
}

if(window.addEventListener){
window.addEventListener('load',multipleTest,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',multipleTest);
}
}
</script>

</head>
<body>

<h4>Mouseover the required options but <span>do not click them</span>.<br>
This will not work in IE browsers.</h4>

<form action="http://www.google.com">
<div>
<select name="alphabet" multiple="multiple" size="21">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
<option value="f">f</option>
<option value="g">g</option>
<option value="h">h</option>
<option value="i">i</option>
<option value="j">j</option>
<option value="k">k</option>
<option value="l">l</option>
<option value="m">m</option>
<option value="n">n</option>
<option value="o">o</option>
<option value="p">p</option>
<option value="q">q</option>
<option value="r">r</option>
<option value="s">s</option>
<option value="t">t</option>
<option value="u">u</option>
</select>
<input type="submit">
<input type="reset">
</div>
</form>

<div id="info">&nbsp;</div>

</body>
</html>

Clueful
06-28-2009, 09:06 PM
Hi,
Does anyone know how to get a <select multiple="true"> to allow the user to select multiple items, without the need of holding the CTRL key?
Yep<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Mouse-Only Multi Selections</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>
<select multiple id='sm' size='6'>
<option>To make
<option>Multiple
<option>Selections
<option>Just Click
<option>With The
<option>Mouse
</select>
<select multiple id='sm2' size='6'>
<option>Everything
<option>Is
<option>Possible
<option>Until
<option>Proven
<option>Otherwise
</select>

<script type='text/javascript'>

function multiSelect( elemId )
{
var table = [], box = document.getElementById(elemId), lastSelected = -1, tempIdx, ctrl = false;

box.onkeyup = box.onkeydown = readCtrlKey;

box.onclick = function( /*28432953637269707465726C61746976652E636F6D*/ )
{
if( !ctrl )
{
if( !inTable( this.selectedIndex ) )
table.push( this.selectedIndex );
else
removeFromTable( this.selectedIndex );

tempIdx = this.selectedIndex;

for( var i=0, len = this.options.length; i<len ; i++ )
if( i != this.selectedIndex )
this.options[ i ].selected = inTable( i );

this.options[ this.selectedIndex ].selected = inTable( this.selectedIndex );

lastSelected = tempIdx;
}
}

function removeFromTable( idx )
{
for( var i=0, len=table.length; i<len && table[i]!=idx; i++ )
;
if(i != len)
table.splice(i, 1);
}

function inTable( idx )
{
for( var i=0, len=table.length; i<len && table[i]!=idx; i++ )
;
return i != len;
}

function readCtrlKey( evt )
{
var e = evt || window.event;

if( typeof( e.ctrlKey ) != 'undefined')
ctrl = e.ctrlKey;
}
}

multiSelect( "sm" );
multiSelect( "sm2" );

</script>

</body>
</html>

paul_norman_81
06-29-2009, 06:26 AM
I was intrigued by this thread (normall stay out of the JS forum), but I found Clueful's version to be buggy when you click a few in one select, then click some in another, then hold ctrl and click one of the ones from the first again - sometimes all disappear...

I wrote my own anyway... May or may not work in older browsers, it was just for fun.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Select Test</title>

<meta name="keywords" content="" />
<meta name="description" content="" />

<script type="text/javascript">

// Variables we need
var previous = new Array();
var lastClicked = '';

// We are going to attach event listeners, no code at the bottom or anything hard coded...
function addEvent(obj, evType, fn)
{
if(obj.addEventListener)
{
obj.addEventListener(evType, fn, false);
return true;
}
else if(obj.attachEvent)
{
var r = obj.attachEvent('on' + evType, fn);
return r;
}
else
{
return false;
}
}

// Let's begin when the DOM is ready
addEvent(window, 'load', begin);

// Attach the handlers to our selects
function begin()
{
addSelect('numbers');
addSelect('sm');
addSelect('sm2');
}

// We add a couple of handlers to each
function addSelect(id)
{
var sel = document.getElementById(id);
addEvent(sel, 'click', whichElement);
addEvent(sel, 'click', addRemoveClicked);
}

// Find which element we are looking at on this click
function whichElement(e)
{
if(!e)
{
var e = window.event;
}

if(e.target)
{
lastClicked = e.target;
}
else if(e.srcElement)
{
lastClicked = e.srcElement;
}

if(lastClicked.nodeType == 3) // Safari bug
{
lastClicked = lastClicked.parentNode;
}
}

// Make sure we are displaying the correct items
function addRemoveClicked(e)
{
if(!previous[this.id])
{
previous[this.id] = new Array();
}

// Remember what has been used
if(previous[this.id][lastClicked.value] == 1)
{
previous[this.id][lastClicked.value] = 0;
}
else
{
previous[this.id][lastClicked.value] = 1;
}

var selectBox = document.getElementById(this.id);

// Add correct highlighting
for(var i = 0; i < selectBox.options.length; i++)
{
selectBox.options[i].selected = '';

if(previous[this.id][selectBox.options[i].value] == 1)
{
selectBox.options[i].selected = 'selected';
}
}
}
</script>
</head>

<body>
<form name="selectTest" action="">

<select name="numbers" id="numbers" multiple="multiple" size="6">
<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>
</select>

<select name="sm" id="sm" multiple="multiple" size="6">
<option value="a">To make</option>
<option value="b">Multiple</option>
<option value="c">Selections</option>
<option value="d">Just Click</option>
<option value="e">With The</option>
<option value="f">Mouse</option>
</select>

<select name="sm2" id="sm2" multiple="multiple" size="6">
<option>Everything</option>
<option>Is</option>
<option>Possible</option>
<option>Until</option>
<option>Proven</option>
<option>Otherwise</option>
</select>

</form>
</body>
</html>

mattmill30
06-30-2009, 04:56 PM
Wow!

Lol, so much for thinking it was a html command that i hadn't thought of.

Thanks everyone for getting back to me.

paul_norman_81 works really well, and i've tried mixing the commands, like holding the CTRL and clicking and it compensates.

Thanks for the help!

I clearly need to go and buy the idiots guide to javascript :P

mattmill30
06-30-2009, 05:04 PM
Just out of curiosity, paul_norman_81, rather than manually listing each of the <select> elements into the addSelect section, could the code be adapted to detect which select is being clicked on?

mattmill30
06-30-2009, 06:43 PM
hmm, i think i've found a bug in a browser.

Was this script tested in Konqueror?

Because, in Konqueror, when i select an item, and then press CTRL and try to select another item, it de-selects the first item selected.

Ok, its quite a complex one, lol:
Part 1:
if i select 1, 2, 4 and 5, then select 3 with CTRL held down, it will deselect 1 on the first click and then 2 on the second click, on the third click it will select 3, on the forth click it will deselect number 4 and on the first click it will deselect number 5.

Part 2:
Whilst holding CTRL number 3 can't be deselected with a single click.

Part 3:
Whilst holding CTRL number 3 can be deselected with a double click, however only once all the items selected above it, have been deselected (using the process highlighted in part 1 or manually deselecting the above items without holding CTRL).

Any ideas?

TIA :)

Matthew Millar

paul_norman_81
07-01-2009, 08:56 AM
Just out of curiosity, paul_norman_81, rather than manually listing each of the <select> elements into the addSelect section, could the code be adapted to detect which select is being clicked on?

You could attach the handlers to all select boxes with multiple="true" on them... Just update the 'begin' function to this:


// Attach the handlers to our selects
function begin()
{
var selects = document.getElementsByTagName('select');

for(var i = 0; i < selects.length; i++)
{
if(selects[i].multiple == true)
{
addEvent(selects[i], 'click', whichElement);
addEvent(selects[i], 'click', addRemoveClicked);
}
}
}


And you can remove the 'addSelect' function too.

Regarding browser testing, I'm afraid I didn't do any! It should work in all decent browsers, I'll check out the Linux based ones this afternoon (assuming I get time).

mattmill30
07-01-2009, 03:56 PM
Hi paul_norman_81,

thanks for getting back to me.

I have no idea why, but for some reason, replacing the original begin() with the new begin() has fixed the problem i listed above.

Any ideas why this is?

Thanks for all the help,

Matthew Millar

mattmill30
07-01-2009, 05:09 PM
Sorry ignore the last post, i'd actually disabled the feature, hence why using CTRL worked. lol.
Sorry about that.

mattmill30
07-11-2009, 09:46 AM
does anyone know about the konquer problem mentioned in post #10?

Thanks,

Matthew Millar