Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery find option in Select with empty/null value remove it and select the first option after it with a value

Tags:

jquery

forms

All in all I want to remove any and all empty options from a SELECT input. The select is dynamically generated from a script someone else wrote, based on an object it receives. My problem is sometimes the object has empty values, leaving me with a select option set that leaves something to be desired.

Anyway almost every time the first option in the set is empty. So I want to remove that and apply selected="selected" to the first option that comes up after the first one is removed., while removing any other possibilities that have empty values.

So in testing the initial concept I came up with

$(select).each(function(){
                if ($(this+' option:selected').val() == '')
                {
                    //this assumes that your empty value is ''
                    $(this).remove();
                }
            });

which seems to remove the entire select, and I know its cause this is defined as the select itself.

I've also tried $(select +'option[value=""]').remove();

Currently I am only trying to get it to remove the options with the empty values, after I can achieve that then I will figure out how I to select the first item on the list officially as far as the DOM would be concerned.

also worth mentioning 'select' the variable being passed is defined previously in the function so it is working for that part of the notion. Just sayin so no one thinks I am some how confusing it with something else.

like image 390
chris Avatar asked Jun 13 '12 20:06

chris


People also ask

How do I get the first select option in jQuery selected?

Select the <select> element using JQuery selector. This selector is more specific and selecting the first element using option:nth-child(1). This will get access to the first element (Index starts with 1).

How do I select a specific Dropdownlist using jQuery?

Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).

How do you remove all options from select in jQuery except first?

Explanation: If we want to remove all items from dropdown except the first item then we can use $('#ddlItems option:not(:first)'). remove(); Here we have excluded first item from being deleted. If we want to remove all items from dropdown except the last item then we can use $('#ddlItems option:not(:last)').

How do I remove a selected value from a Dropdownlist?

The option to be removed is selected by getting the select box. The value to be removed is specified on the value selector (value='optionValue') on the select box. The remove() method is then used to remove this selected option. The find() method can be used to find the option in the value with the value selector.


1 Answers

  • you can use .filter() to filter out the empty option elements and remove them all at once

  • use .prop() to set special attributes like selected

Code:

$('select option')
    .filter(function() {
        return !this.value || $.trim(this.value).length == 0;
    })
   .remove();

$('select option')
    .first()
    .prop('selected', true);​

DEMO

like image 171
Didier Ghys Avatar answered Oct 06 '22 00:10

Didier Ghys