0

I have two dropdowns DD1 & DD2 , I want that if user selects option HELLO (value="100") in DD1 and presses a key , then the same value option in DD2 is removed, DD1 will have that option, it would only be removed from DD2

var selectedItems = $('#DD1').val();


 for ( var i = 0 ; i < selectedItems.length; i++) 
        {
           $("#DD2 option[value='"+selectedItems[i]+']"').remove();
        }                                                                       
2
  • 3
    What have you tried so far? Post your code. Which keyboard key are they going to be pressing?
    – Treffynnon
    Jan 13, 2012 at 9:48
  • @Treffynnon :any key sir, lets take it as 9
    – sqlchild
    Jan 13, 2012 at 9:48

2 Answers 2

0

This does not handle the keypress event, but this is what could go in the callback when you figure out the specifics of the event you want to listen for.

var dd1 = $("#DD1").val();

$("option[value='"+dd1+"']", $("#DD2")).detach();
2
  • sir, the ids are DD1 and DD2 , so in place of $(DD1-Selector-Here).val() , I would have to write $('#DD1').val(); i guess ?
    – sqlchild
    Jan 13, 2012 at 10:00
  • sir, i have posted what i was doing
    – sqlchild
    Jan 13, 2012 at 10:13
0

Mine is working now ;

$('#DD1').bind('keypress', function(e) { 

if ( e.which == 19)

{ 
var selectedItems = $('#DD1').val();


 for ( var i = 0 ; i < selectedItems.length; i++) 
        {
           $("#DD2 option[value='"+selectedItems[i]+"']").remove();
        } 
}  
 }
3
  • So you're checking for the "Pause/Break" Key to be down, because e.which == 19 is the "Pause/Break" key on most "Standard" keyboards (tested on several different keyboards including dell package and gateway package kb's). Just trying to clarify what you are doing and its function so maybe i could help in a shorter code manner. (see my which list here -> forum.jquery.com/topic/eventwhich-code-list-just-for-help)
    – SpYk3HH
    Jan 13, 2012 at 17:31
  • uhm, I program on 3 different systems (basically for cross-checking and bencchmarking) and I just checked, using a simple fiddle form I have and got the same as I always have, 19=Puase and 13=Enter.
    – SpYk3HH
    Jan 16, 2012 at 15:43
  • @SpYk3HH is correct, 13 is the keycode for enter...more importantly, why would you require a keypress? Is the user going to find the selection they want and then press enter? That is not usually how a dropdown is used. Why not just listen for the change event?
    – Jonathan
    Jan 23, 2012 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.