3

On my jQuery Mobile website I would like to give the user the possibility to add select menus on click. The additional select menus appear, but do not work. No options are shown when the select menus are being clicked.

Here comes my code:

JSFiddle: http://jsfiddle.net/nCs6U/82/

HTML:

<div id="select-con">
    <div class="select-row">
        <form class="select-form">
            <select class="select-class" name="select-name"
                    data-native-menu="false">
                <option value="0" data-placeholder="Choose">Choose</option>
                <option value="1">Option 1</option>
                <option value="2" selected="selected">Option 2</option>
                <option value="3">Option 3</option>
            </select>
        </form>
    </div>

    <!-- raw select menu for adding -->
    <div id="select-row-raw" class="select-row">
        <form class="select-form">
            <select class="select-class" name="select-name"
                    data-native-menu="false">
                <option value="0" data-placeholder="Choose">Choose</option>
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
                <option value="3">Option 3</option>
            </select>
        </form>
    </div>
</div>

<button>Add Select</button>

JS:

$('button').on('click', function() {
    $clone = $('#select-row-raw').clone();
    $clone.appendTo('#select-con').show();
});

How do I achieve that the added select menus (clones of "#select-row-raw") work?

* UPDATE *

New fiddle: jsfiddle.net/nCs6U/85

The weird thing is that for the first added select menu the display of options still does not work. How can this problem be solved?

Max
  • 832
  • 1
  • 14
  • 33

2 Answers2

1

In your JS try adding to your clone function like so:

$('button').on('click', function() {
    $clone = $('#select-row-raw').clone(true, true);
    $clone.appendTo('#select-con').show();
});

This will clone the event listeners of #select-row-raw and it's children

Jonathon Blok
  • 749
  • 4
  • 14
  • almost perfect. for the first added select menu, the options still are not shown. updated fiddle: http://jsfiddle.net/nCs6U/85/ any idea how to fix this problem? – Max Jul 08 '15 at 09:09
  • @Max Sorry, I couldn't work this out. Did you solve it in the end? – Jonathon Blok Jul 10 '15 at 11:57
0

In jQuery.clone() you have to set the event parameters to true as per this ref

$element.clone(true, true);

clone( [withDataAndEvents] [, deepWithDataAndEvents] )

withDataAndEvents: A Boolean indicating whether event handlers and data should be copied along with the elements. The default value is false.

deepWithDataAndEvents: A Boolean indicating whether event handlers and data for all children of the cloned element should be copied. By default its value matches the first argument's value (which defaults to false).

plus the suggestion from @jqueryKing, to have unique HTML ID's. So change your code to:-

HTML

<div id="select-con">
    <div class="select-row">
        <form class="select-form">
            <select class="select-class" name="select-name"
                    data-native-menu="false">
                <option value="0" data-placeholder="Choose">Choose</option>
                <option value="1">Option 1</option>
                <option value="2" selected="selected">Option 2</option>
                <option value="3">Option 3</option>
            </select>
        </form>
    </div>

    <!-- raw select menu for adding -->
    <div class="select-row">
        <form class="select-form">
            <select class="select-class" name="select-name"
                    data-native-menu="false">
                <option value="0" data-placeholder="Choose">Choose</option>
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
                <option value="3">Option 3</option>
            </select>
        </form>
    </div>
</div>
<button>Add Select</button>

JS

$('button').on('click', function() {
    // To avoid duplicate clones, use .first()
    // To enable bindings, pass (true,true) to the clone function
    $clone = $('.select-row').first().clone(true,true); 
    $clone.appendTo('#select-con').show();
});
Community
  • 1
  • 1
Roshdy
  • 1,703
  • 3
  • 25
  • 34