I am dynamically creating input elements using jquery, but when I try to access the dynamically created element values, I'm getting either empty
or undefined
or . I tried many ways to get the values, but It is failing.
Could someone help me getting this done.
Here is the code
$(document).ready(function() {
var count = 0;
var jsondata = [];
var selectedField;
$('.add').click(function() {
count = count + 1;
jQuery('<div/>', {
id: 'form-wrapper' + count,
class: 'form-wrapper' + count,
title: 'now this div has a title!'
}).appendTo('.wrapper0');
$('.form-wrapper' + count).append("<label>FieldName</label> <input type='text' placeholder='label' id='label" + count + "'></input> <select class='select" + count + "'><option>Select</option><option value='textbox'>Textbox</option><option value='textarea'>TextArea</option><option value='checkbox'>Checkbox</option><option value='radio'>Radio</option><option value='date'>Date</option> </select><input type='text' id='commavalues" + count + "' disabled placeholder='Enter comma(,) seperated values'></input><input type='checkbox' id='required" + count + "'>Required</input><button class='delete'>Delete</button> <br><br>");
$('.delete').click(function(e) {
($(this).parent().remove());
});
$('.select' + count).change(function() {
selectedField = $(this).children('option:selected').val();
if (selectedField == 'checkbox' || selectedField == 'radio') {
$('#commavalues' + count).prop('disabled', false);
flag = 1;
} else {
flag = 0;
$('#commavalues' + count).prop('disabled', true);
}
});
item = {};
var label = $('#label' + count).val();
var required = $('#required' + count).is(":checked");
var label_type = selectedField;
var options = $('#commavalues' + count).val();
console.log(label);
console.log(required);
console.log(label_type);
console.log(options);
item["label"] = label;
item["label_type"] = label_type;
item["options"] = options;
item["required"] = required;
jsondata.push(item);
});
$('.save').click(function() {
console.log(jsondata);
});
});
body {
background-color: #ECEFF1;
margin: 50px 0;
padding: 0;
text-align: center;
}
button {
position: relative;
margin-left: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper0">
<div class="form-wrapper">
<label>FieldName</label>
<input type="text" placeholder="label" class="label0"></input>
<select class="select0">
<option>Select</option>
<option value="textbox">Textbox</option>
<option value="textarea">TextArea</option>
<option value="checkbox">Checkbox</option>
<option value="radio">Radio</option>
<option value="date">Date</option>
</select>
<input type="text" class="commavalues0" disabled placeholder="Enter comma(,) seperated values"></input>
<input type="checkbox" class="required0">Required</input>
<br><br>
</div>
</div>
<button class="add">Add</button>
<button class="save">Save</button>
I am trying to display in JSON format when I click save
button, and If i click the delete button the data associated to that input fields are to be deleted also.