user2689642
user2689642

Reputation: 65

Identifying a Select option by using only select name and options value

<select name="option[239]" onchange="jsFunction(this)">
    <option value="">--- Please Select ---</option>
    <option value="49">Double Standard</option>
    <option value="50">Single Express</option>
    <option value="51">Single Standard</option>
    <option value="48">Double Express</option>
</select>
<select name="option[240]" onchange="jsFunction(this)">
    <option value="">--- Please Select ---</option>
    <option value="49">Double Standard</option>
    <option value="50">Single Express</option>
    <option value="51">Single Standard</option>
    <option value="48">Double Express</option>
</select>

Html

function jsFunction(element){
    alert("You changed to " + element.value );
    if (element.value=='50') {
        $('[name="option[240]"]').remove();
    }
}

Script

Ok I can get it to remove the entire select option[240] however I don't know how I would remove one of the options from 240 (e.g 49 is removed from option[240] if 50 is selected)

Upvotes: 1

Views: 115

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

Since element refers to the select element, you can use it to find the option to be removed.

If you want to remove predefined elements then

function jsFunction(element) {
    var $select = $(element);

    if ($select.val() == '50') {
        $select.find('option[value=49]').remove()
    }
}

Demo: Fiddle

If you want to remove all the option before the selected one then

function jsFunction(element) {
    var $select = $(element);
    var idx = $select.find('option:selected').index();
    $select.find('option').slice(1, idx).remove()
}

Demo: Fiddle

Upvotes: 2

Sridhar R
Sridhar R

Reputation: 20408

Try this

HTML

<select id="test" name="option[240]" onchange="jsFunction(this)">
    <option value="">--- Please Select ---</option>
    <option value="49">Double Standard</option>
    <option value="50">Single Express</option>
    <option value="51">Single Standard</option>
    <option value="48">Double Express</option>
</select>

Script

$("#test option[value='49']").remove();//it will remove value 49

Upvotes: 2

Related Questions