Jump to content
Science Forums

The Selector


Recommended Posts

So last week i needed to create a fun way for people to be able to easily use extensive selections lists. And while i could just list it on a huge page (i think i have something like 300 items) i didnt want to clutter the page too much with stuff that may or may not be relevant, so i sat around and came up with this idea. Create a multiple selection selection box, then list the selected values in an updated list below, allowing for an easy deselection of any items. I figured that the most common use for the list would be to either select a few items, or to select all the items on the list, and deselect a few that you dont need, so this is the setup for the code below and why this selector made sense, check it out, mod it, break it, use it. In my final version this does some ajax requests, but as a proof of concept, i think this worked out pretty well

 

<html>
<head>
<title>Test</title>
<script src='http://code.jquery.com/jquery-1.5.min.js' type='text/javascript'></script>
<script type="text/javascript">
$(document).ready(function () {
$('.mselect').change(function () {
		var selected= "";
		$("option:selected",this).each(function()
		{
			selected+="<span class='remove "+$(this).val()+"' onmouseover=\"this.style.background='#FFB495';\" onmouseout=\"this.style.background='none';\"'>"+$(this).text() + "</span><br/>";
		});
		$('.mtselect').html(selected);
});

$('.remove').live('click', function() {
	which='.mselect option:eq('+$(this).attr('class').split(' ').slice(-1).toString()+')';
	$(which).attr("selected",false);
	$('.mselect').trigger('change');
});	
$('.select_all').click(function() {
	$('.mselect option:not(:selected)').each( function() {
		$(this).attr("selected", true);
	});
	$('.mselect').trigger('change');
});
});
</script>
<style type="text/css">
.mtselect option:hover {
   background-color:#ccc;
}
</style>
</head>
<body>
<select multiple="multiple" size="5" name="selected[]" class="mselect" style="width: 20%">
   <option value=0>one</option>
   <option value=1>two</option>
   <option value=2>three</option>
   <option value=3>four</option>
   <option value=4>five</option>
   <option value=5>six</option>
</select>
<div class="select_all" style="cursor: pointer; cursor: hand">Select All</div>
<br /><br />
Click to remove:
<br /><br />
   <div class="mtselect" style="cursor: pointer; cursor: hand"></div>
</body>
</html>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...