1

Is there a chance to have a combobox, which every time I select something in there, it opens a new one, etc, until no more data to select?

(options must not be repeated)

Combo1 - (opt1-opt2-opt3) - selected opt1
Combo2 - (opt2-opt3) - selected opt3
Combo3 - (opt2)

Comboboxes should be populated by querys using php and mysql.

How can I do it ? Cheers!

4
  • check out this link Commented Nov 22, 2012 at 11:17
  • and another example here Commented Nov 22, 2012 at 11:19
  • @NikosTsirakis, first dont work Commented Nov 22, 2012 at 11:26
  • Try to create a sample code and give us your try here and we can assist you further if it doesn't work. Commented Nov 22, 2012 at 12:13

1 Answer 1

3

Here is some jquery that will do this:

HTML

<select id="combo1" class="combo" data-index="1">
    <option></option>
    <option val="Opt1">Opt1</option>
    <option val="Opt2">Opt2</option>
    <option val="Opt3">Opt3</option>
</select>

Javascript

$('body').on('change', '.combo', function() {
    var selectedValue = $(this).val();

    if ($(this).find('option').size() > 2) {
        var newComboBox = $(this).clone();
        var thisComboBoxIndex = parseInt($(this).attr('data-index'), 10);
        var newComboBoxIndex = thisComboBoxIndex + 1;

        $('.parentCombo' + thisComboBoxIndex).remove();

        if (selectedValue !== '') {
            newComboBox.attr('data-index', newComboBoxIndex);
            newComboBox.attr('id', 'combo' + newComboBoxIndex);
            newComboBox.addClass('parentCombo' + thisComboBoxIndex);
            newComboBox.find('option[val="' + selectedValue + '"]').remove();
            $('body').append(newComboBox);
        }
    }
});​

Demo

Explanation

// Adds an event listener to each combo box that is created dynmically
$('body').on('change', '.combo', function() {
    // Gets this selected value
    var selectedValue = $(this).val();

    // Checks if there are enough options left to create another combo box
    if (selectedValue !== '' && $(this).find('option').size() > 2) {
        // Clones the just selected combo box and get the current and next combo index
        var newComboBox = $(this).clone();
        var thisComboBoxIndex = parseInt($(this).attr('data-index'), 10);
        var newComboBoxIndex = thisComboBoxIndex + 1;

        // Removes any "children" combo boxes
        $('.parentCombo' + thisComboBoxIndex).remove();

        // Checks if a blank value is not selected to create another combo box
        if (selectedValue !== '' && $(this).find('option').size() > 2) {
            // Gives this new combo box the correct id, index, parent class
            newComboBox.attr('data-index', newComboBoxIndex);
            newComboBox.attr('id', 'combo' + newComboBoxIndex);
            newComboBox.addClass('parentCombo' + thisComboBoxIndex);

            // Removes the selected option from the new combo box
            newComboBox.find('option[val="' + selectedValue + '"]').remove();

            // Adds the combo box to the page. Disco!
            $('body').append(newComboBox);
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

@3dgoo, im seeing your example, but when user deselect a combobox it stays blank and should disappear right?
@user1148875 - Yeah you are right. I missed this bug. I've fixed this up now and updated the code. Good pick up. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.