开发者

linked comboboxes in html

开发者 https://www.devze.com 2022-12-26 11:35 出处:网络
Basically my webpage has two comboboxes. First combo box is populated with data comming from MySql. There is a button Add to the side of combo box and when user selectes a item in combo box 1 , clicks

Basically my webpage has two comboboxes. First combo box is populated with data comming from MySql. There is a button Add to the side of combo box and when user selectes a item in combo box 1 , clicks on the Add button, that item should get added to the second combo box tha开发者_开发技巧t is in the same page. Can anyone please tell me how to do this in javascript? or anything?

By the way my web pages are PHP pages.

Thanks.


Hey. You could use a Javascript function like this:

function moveSelectedOption() {
    // Fetch references to the <select> elements.
    var origin = document.getElementById('origin_select');
    var target = document.getElementById('target_select');

    // Fetch the selected option and clone it.
    var option = origin.options[origin.selectedIndex];
    var copy = option.cloneNode(true);

    // Add the clone to the target element.
    target.add(copy, null);
}

Then you just add a call to it to the button's onclick event.

<button onclick="moveSelectedOption()">Add</button>

If you want to move it rather than copy it, remove the cloneNode line and just add the original option.

0

精彩评论

暂无评论...
验证码 换一张
取 消