Moving Select Options Up And Down Via Jquery
Solution 1:
Your code for changing the options works fine. It seems IE8 isn't handling a "fast" second-click with the click
event but rather expects a double click
to be handled.
Using my test code below, you'll notice that in IE8 writes out the following when Move Down/Up
is pressed "fast":
Move Down Click
Move Down Double-Click
Move Down Click
Move Down Double-Click
But with FF/Chrome the following is printed:
Move Down Click
Move Down Click
Move Down Double-Click
Move Down Click
Move Down Click
Move Down Double-Click
Here's the code I'm using to test. I haven't done any tests to see if it's jQuery's event binders that are causing the issues.
<!DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Test</title><scripttype="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script></head><body><selectid="list"multiple="multiple"><optionvalue="option1">Option 1</option><optionvalue="option2">Option 2</option><optionvalue="option3">Option 3</option></select><buttonid="mup">Move Up</button><buttonid="mdown">Move Down</button><scripttype="text/javascript">var $DEBUG = null;
$(document).ready(function ()
{
$DEBUG = $("#debug");
$DEBUG.append("Registering Events<br />");
$("#mup").click(moveUpItem);
$("#mdown").click(moveDownItem);
$("#mup").bind("dblclick", function ()
{
$DEBUG.append("Move Up Double-Click<br />");
});
$("#mdown").bind("dblclick", function ()
{
$DEBUG.append("Move Down Double-Click<br />");
});
});
functionmoveUpItem()
{
$DEBUG.append("Move Up Click<br />");
}
functionmoveDownItem()
{
$DEBUG.append("Move Down Click<br />");
}
</script><divid="debug"style="border: 1px solid red"></div></body></html>
EDIT: I can confirm it is IE8 that is the problem. Swap this IE8-specific code in the $(document).ready() handler:
// $("#mup").click(moveUpItem);
$("#mup")[0].attachEvent("onclick", moveUpItem);
// $("#mdown").click(moveDownItem);
$("#mdown")[0].attachEvent("onclick", moveUpItem);
// $("#mup").bind("dblclick", function()
$("#mup")[0].attachEvent("ondblclick", function()
{
$DEBUG.append("Move Up Double-Click<br />");
});
// $("#mdown").bind("dblclick", function()
$("#mdown")[0].attachEvent("ondblclick", function()
{
$DEBUG.append("Move Down Double-Click<br />");
});
Solution 2:
Example: to move 3rd option before 1st option, we can use below jquery:
$('select[name="NameOfDropDown"] option:eq(2)').insertBefore($('select[name="NameOfDropDown"] option:eq(0)'));
Solution 3:
I think this will give some ideas of how to do it, you can dynamically place any option before one known position just by knowing the values of both, the one to move and the one of the position:
How would you dynamically order an <option select> list using JQuery?
Solution 4:
I know this one is a bit old, but I recently made this simple jQuery plugin to be able to reorder elements in a multiple select element.
Have a look and see if it helps for you, I tested in IE8,IE9,Chrome,FireFox,Opera.
Post a Comment for "Moving Select Options Up And Down Via Jquery"