user3675188
user3675188

Reputation: 7409

How could I dynamically change dropdown select content

Like an airline website.

When you select the departure airport.

the content of arrival airport will be changed correspondinly.

For examle, if you choose Paris at departure airport. Then, Paris should not appear in the arrival airport.

How could I do this, any keyword or term to search for further information ?

Thanks

Upvotes: 1

Views: 80

Answers (2)

Joy
Joy

Reputation: 9550

Check working demo: JSFiddle

Use jQuery selector to find the destination options element, iterate to hide the one targeted:

function changeDestination(target) {
    var options = $('#destination option');
    for (var i = 0; i < options.length; i++) {
        var opt = $(options[i]);
        opt.show();

        if (opt.attr('name') === target.value) {
            opt.hide();
        }
    }
}

Upvotes: 2

Skinner
Skinner

Reputation: 1503

This is how you dynamically add an Option to a select....

var x = document.getElementById("selectid");
var option = document.createElement("option");
option.text = "newOption";
x.add(option);

There is also a remove() method you can employ.

I think a combination of those will get done what you need to get done. Google "add option to select javascript" and "remove option from select javascript" and i think you will be on your way.

Upvotes: 0

Related Questions