0

I am adding dynamically multiple divs which contain select options and want to remove the option which is selected in first select from the next select box.

Please check the jdfiddle here: http://jsfiddle.net/MSvje/

<div class="center">
    <div class="row-fluid">
        <div class="span3">
            <select class="chosen-select" id="form-field-select-3" data-placeholder="Choose a Country...">
                <option value="">Text</option>
                <option value="AL">Department</option>
                <option value="AK">City</option>
                <option value="AZ">State</option>
                <option value="AR">Country</option>
                <option value="CA">Industry</option>
            </select>
        </div>
    </div>
</div>
<a href="javascript:void(0);" id="search-add"><i class="icon-plus">Add One</i></a>
<br />
<a href="javascript:void(0);"  id="search-remove"><i class="icon-minus">Remove One</i></a>

Js CODE:

jQuery(function($) {
        $("#search-add").click(function () {
            if( ($('.center .row-fluid').length+1) > 5) {
                alert("Only 5  allowed");
                return false;
            }
            var id = ($('.center .row-fluid').length + 1).toString();
            $('.center').append('<div class="row-fluid"><div class="span3"><select class="chosen-select" id="form-field-select-3" data-placeholder="Choose a Country..."><option value="">Text</option><option value="AL">Department</option><option value="HI">City</option><option value="HI">State</option><option value="HI">Country</option><option value="HI">Industry</option></select></div>'); 
                });

                $("#search-remove").click(function () {
                    if ($('.center .row-fluid').length == 1) {
                        alert("No more textbox to remove");
                        return false;
                    }
                    $(".center .row-fluid:last").remove();
                });
            });
1
  • apart from having multiple id problem in your code.. your question is not clear Commented Oct 5, 2013 at 18:38

1 Answer 1

1

You have a problem with multiple IDs. Id has to be unique.

Try this:

var clone = $('.row-fluid').last().clone();
clone.find('#form-field-select-3').prop('id','clone_'+ids);
clone.find('option:selected').remove()

Demo here

Sign up to request clarification or add additional context in comments.

Comments

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.