Cloning Form elements - select and range inputs
I am having a hell of a time cloning a form in jQuery Mobile 1.3.2 and jQuery 1.9.1. Originally all expandable and range inputs weren't working (were visible but had no functionality) when cloned, now they are acessible but duplicated - a select nested inside a select
- I have the following markup:
- <div data-role="page" id="auditForm" data-theme="a">
- <!--Observation-->
- <div class="observationTemplate">
- <h4 class="observationTitle">Observation - 1</h4>
- <a href="#" data-role="button" data-icon="delete" data-iconpos="notext" data-inline="true" data-theme="b" class="close">close</a>
- <div data-role="fieldcontain">
- <label for="audit-observation-category" class="ui-hidden-accessible select">Observation Category</label>
- <select name="audit-observation-category" id="audit-observation-category" data-theme="e" data-corners="false">
- <option value="" selected>Observation Category</option>
- <option value="Site">Site</option>
- <option value="Incident">Incident</option>
- <option value="NearMiss">Near Miss</option>
- </select>
- </div>
- <div data-role="fieldcontain">
- <label class="ui-hidden-accessible" for="audit-observation-notes">Notes</label>
- <textarea cols="40" rows="8" name="audit-observation-notes" id="audit-observation-notes" class="notes" placeholder="Notes" maxlength="140"></textarea>
- </div>
- <div data-role="fieldcontain">
- <label class="ui-hidden-accessible" for="audit-observation-recommendation">Recommendation</label>
- <textarea cols="40" rows="8" name="audit-observation-recommendation" id="audit-observation-recommendation" class="notes" placeholder="Recommendation" maxlength="140"></textarea>
- </div>
- <!-- not working for some reason -->
- <div data-role="fieldcontain">
- <label for="audit-observation-severity" data-theme="e">Severity</label>
- </div>
- <div data-role="fieldcontain">
- <input type="range" name="audit-observation-severity" id="audit-observation-severity" value="15" min="15" max="180" step="15" data-hilight="true" data-theme="d">
- </div>
- <div data-role="fieldcontain">
- <label for="audit-observation-person" class="ui-hidden-accessible select">Observation Person</label>
- <select name="audit-observation-person" id="audit-observation-person" data-theme="e" data-corners="false">
- <option value="" selected>Observation Person</option>
- <option value="Jim">Jim</option>
- <option value="Bob">Bob</option>
- <option value="Gary">Gary</option>
- </select>
- </div>
- <div data-role="fieldcontain">
- <label class="ui-hidden-accessible" for="audit-observation-date">Date</label>
- <input type="date" name="audit-observation-date" id="audit-observation-date" placeholder="Date" value="">
- </div>
- </div><!--/observation-->
- <div id="auditContainer"></div>
- <div data-role="controlgroup" data-type="horizontal" data-theme="d">
- <a href="#" id="auditObservationButton" data-role="button" data-icon="plus" data-iconpos="right" data-inline="true" data-theme="b">Add Observation</a>
- </div>
- </div>
- and the following jQuery
- function newObservation() {
- var len = $('.observation').length;
- var titleLen = $('.observation').length + 2;
- var $html = $('.observationTemplate').clone();
- $('.observationTitle:eq(0)').text("Observation - " + titleLen);
- $html.find('[name=audit-observation-category]').eq(0).attr({name:"audit-observation-category" + len, id:"audit-observation-category" + len});
- $html.find('[name=audit-observation-notes]').eq(0).attr({name:"audit-observation-notes" + len, id:"audit-observation-notes" + len});
- $html.find('[name=audit-observation-recommendation]').eq(0).attr({name:"audit-observation-recommendation" + len, id:"audit-observation-recommendation" + len});
- $html.find('[name=audit-observation-severity]').eq(0).attr({name:"audit-observation-severity" + len, id:"audit-observation-severity" + len});
- $html.find('[name=audit-observation-person]').eq(0).attr({name:"audit-observation-person" + len, id:"audit-observation-person" + len});
- $html.find('[name=audit-observation-date]').eq(0).attr({name:"audit-observation-date" + len, id:"audit-observation-date" + len});
- return $html.html();
- }
- $(document).on('pagebeforecreate', function(){
- $('<div/>', {
- 'class' : 'observation', html:newObservation()
- }).appendTo('#auditContainer');
- $('#auditObservationButton').click(function() {
- $('<div/>', {
- 'class':'observation', html:newObservation()
- }).hide().appendTo('#auditContainer').slideDown('slow');
- $('#auditContainer').trigger('create');
- $('select').selectmenu('refresh');
- });
- $('#auditForm').on('click', '.close', function(){
- $(this).parent().fadeOut();
- });
- });
I have a
fiddle, but for some reason it doesn't like jqm 1.3.2 and have it on my dev server
http://dirtybirddesign.com/dirtybirddesignlab.com/FireSafe/audit.html#auditForm
Any help would be much appreciated.
Kane Leins