0

I have this code, and I have it removing my first "option" because the first one says "Please Select:". Anyways, problem is, when I focus on my first "select" it removes the first "option" from EVERY select. How do I make it remove it from ONLY the currently focused one?

$(document).ready(function(){

  $('select').live('focus',function() {
   $('.firstoption').remove();
  });

});

2 Answers2

0
$(document).ready(function(){

  $('select:first').on('focus',function() {
   $(this).children("option:first").remove();
  });

});

Example: http://jsfiddle.net/Ky6zq/2/

This will execute every time the select receives focus, so you will keep removing elements every time. To prevent this issue try:

$(document).ready(function(){

  $('select:first').one('focus',function() {
   $(this).children("option:first").remove();
  }); 
});

Example: http://jsfiddle.net/Ky6zq/1/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0
$(document).ready(function(){
   $(document).on('focus', 'select', function() {
       $('.firstoption', this).remove();
   });
});

You should be using the delegated version of on(), not live() which is deprecated.

Make sure you use a class for the first option, so as to not remove an option every time the select receives focus.

adeneo
  • 312,895
  • 29
  • 395
  • 388