Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:25:35+00:00 2026-06-16T15:25:35+00:00

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> jQuery

  • 0

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>

jQuery

$('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);
    }
}
});

Fiddle

This code is working and is creating a new combobox everytime the one before is filled.

How can I have a linebreak every two dropdown? like this:

enter image description here

And how can I force it to fill always an even number of comboboxes. Eg. If it is only 3 combobox filled, must alert error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:25:36+00:00Added an answer on June 16, 2026 at 3:25 pm

    This assumes you put your select boxes in a container that only holds the select boxes and the br’s. (updated with validation)

    $('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);
            }
            $('body br').remove();
            $('body select:odd').after($('<br />'));
        } 
    
        validate();
    });
    
    function validate() {
        var count = $('body select[value!=""]').length;
        if (count > 0 && count % 2 == 0) 
            $('#ok').text('OK!');
        else
            $('#ok').text('Not an even number selected');
    }
    

    ​

    http://jsfiddle.net/P5nV8/4/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Html <div id=opts class=styled-select> <select id=combo1 class=combo data-index=1> <option></option> <option val=Opt1>Opt1</option> <option val=Opt2>Opt2</option> <option
I have 3 select option in my html page like this <select class=combo> <option
I have a html combo/select. When the user selects a element, I want to
I have this HTML dropdown: <form> <input type=text id=realtxt onkeyup=searchSel()> <select id=select name=basic-combo size=1>
I have a classic HTML select box: Show: <select name=show id=showThreads> <option value=all selected=selected>All</option>
The webkit html select element uses a checkmark to denote a selected option. I
I have a html select list menu,when user select an option he/she is redirected
I have problem while loading data into html select when users press or click
I want to create a HTML customized select combo box. What are the libraries
I have html like this: <select name=sortProducts id=sortProduct multiple=multiple> <optgroup label=Availability> <option selected=selected value=option1>Low

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.