Skip to content Skip to sidebar Skip to footer

Chosen Plugin Options Are Not Passed To Cloned Row

SETUP I'm using Chosen plugin (http://harvesthq.github.com/chosen/) and Cloning using relCopy script PROBLEM I can clone rows successfully but options like 'max_selected_options'

Solution 1:

I think events are not assigned to newly cloned elements, even though "true" attribute is specified in the clone method.

Anyway, heres a quick work-around to ensure events are always attached.

New Fiddle : http://jsfiddle.net/KjNb5/4/

Old Fiddle : http://jsfiddle.net/KjNb5/1/

HTML

<div class="more-files-template hidden">
    <label>Select Options</label>
    <select data-placeholder="You may select upto Two options" multiple tabindex="6" style="width: 280px; ">
        <option value=""></option>
        <optgroup label="NFC EAST">
            <option>Dallas Cowboys</option>
            <option>New York Giants</option>
            <option>Philadelphia Eagles</option>
            <option>Washington Redskins</option>
        </optgroup>
        <optgroup label="NFC NORTH">
            <option>Chicago Bears</option>
            <option>Detroit Lions</option>
            <option>Green Bay Packers</option>
            <option>Minnesota Vikings</option>
        </optgroup>
    </select> <a class="remove" href="#">remove</a>

</div>
<form id="form" action="/" method="post">
    <div class="files"></div>
</form>
<p><a href="#" class="copy-link">Add More</a>
</p>
<p><a href="#" class="form-submit-button">Submit form</a>
</p>

JS

$( main );

function main() {

    var fieldNumber = 0;
    var fieldNameAndID = "Opt_";
    var maxFieldsAllowed = 5;

    addField(fieldNumber);

    $(".copy-link").on("click", function (e) {
        e.preventDefault();

        addField(fieldNumber);
    });

    $(".files").on("click", "a", function (e) {
        e.preventDefault();

        fieldNumber = fieldNumber - 1;

        $(this)
            .parent()
            .slideUp(function () {
            $(this).remove();
        });
    });

    $(".form-submit-button").on("click", function (e) {
        e.preventDefault();

        alert($("#form").serialize());
    });


    function addField(index) {

        fieldNumber = index + 1;

        if (fieldNumber > maxFieldsAllowed) {
            fieldNumber = fieldNumber - 1;
            alert("Sorry! cannot add more than " + maxFieldsAllowed + " fields.");
            return false;
        }

        var newFieldNameAndID = fieldNameAndID + fieldNumber;

        var $new = $(".more-files-template")
            .clone(true)
            .removeClass("more-files-template hidden")
            .addClass("another-field");

        if (fieldNumber == 1) {
            $new.find(".remove").remove();
        }

        $(".files").append($new);

        $new.find("select")
            .attr({
            name: newFieldNameAndID,
            id: newFieldNameAndID
        })
            .chosen({
            max_selected_options: 2
        })
            .bind("chosen:maxselected", function () {
            alert("Maximum limit reached");
        });
    }

};

CSS

.hidden {
    display:none; /* to hide template */
}

This isn't prefect but will put you in right direction :)

Hope that helps.

Varinder


Post a Comment for "Chosen Plugin Options Are Not Passed To Cloned Row"