I have this form that the user can input multiple data using dynamic input of jquery. I want to add a new variant when the button is click and add also a dynamic input inside it. My problem is when im adding new data to the newly created variant it also add to the previous variant. I want it to be seperate when im adding value.
`
$(document).ready(function(){
var counter= 0;
function add_variant(counter){
var html='';
html+= '<div class="row" id="variantTitle"><div class="col"><div class="form-group"><label for="variant">Select Variant</label><select class="custom-select border-success variant" name="variant" id="variant"><option value="">Select</option><?php echo fill_variant_box($conn); ?></select></div></div></div>';
return html;
}
function add_input_field(){
$('#testBtn').remove();
$('#cancelBtn').remove();
var html='';
html+='<table class="table table-bordered"><tr><th>Category</th><th>Item Name</th><th>Quantity</th><th>Button</th></tr>';
html+='<tbody class="dynamicAdd"><tr>';
html+='<td><select class="custom-select category border-success" name="category" id="category"><option value="-Select-">-Select-</option><?php echo fill_category_select_box($conn); ?></select></td>'
html+='<td><input class="form-control border-success name_items" id="name_items" name="name_items" autocomplete="off"></td>';
html+='<td><input class="form-control quantity border-success" type="text" name="quantity" id="quantity"></td>';
html+= '<td><button type="button" class="btn btn-success" id="btnAdd">Add</button></td>'
html+='</tr></tbody></table>';
return html;
}
var appendedTable = '<tr>'+
'<td><select class="custom-select category border-success" name="category" id="category"><option value="-Select-">-Select-</option><?php echo fill_category_select_box($conn); ?></select></td>'+
'<td><input class="form-control border-success name_items" id="name_items" name="name_items" autocomplete="off"></td>'+
'<td><input class="form-control quantity border-success" type="text" name="quantity" id="quantity"></td>'+
'<td><button type="button" class="btn btn-danger" id="btnRemove">Remove</button></td></tr>';
$('#add-form').append(add_input_field());
$(document).on('click','#btnRemove', function(){
$(this).closest('tr').remove();
});
$('#add-form').append('<button type="button" class="btn addDonate" id="testBtn">Save</button>');
$('#add-form').append('<button type="button" class="btn cancelBtn" id="cancelBtn">Cancel</button>');
$(document).on('click', '#btnAdd',function(){
$('.dynamicAdd').append(appendedTable);
});
});
then this is the code when im adding new variant
$(document).on('click','#addVar',function(){
function add_input_field(){
$('#testBtn').remove();
$('#cancelBtn').remove();
var html='';
html+='<table class="table table-bordered"><tr><th>Category</th><th>Item Name</th><th>Quantity</th><th>Button</th></tr>';
html+='<tbody class="dynamicAdd"><tr>';
html+='<td><select class="custom-select category border-success" name="category" id="category"><option value="-Select-">-Select-</option><?php echo fill_category_select_box($conn); ?></select></td>'
html+='<td><input class="form-control border-success name_items" id="name_items" name="name_items" autocomplete="off"></td>';
html+='<td><input class="form-control quantity border-success" type="text" name="quantity" id="quantity"></td>';
html+= '<td><button type="button" class="btn btn-success" id="btnAdd">Add</button></td>'
html+='</tr></tbody></table>';
return html;
}
$('#add-form').append(add_input_field());
});
`
I want to create a dynamic input within a dynamic input.
Related
I want to reset the count when the user removes the item. For example, I have these images:
Picture 1
In picture 1, we have Title, Title 1 and Title 2
Picture 2
In picture 2 all the items are removed.
Picture 3
In picture 3, we have Title 3 and Title 4.
What I want is when user clicks on add button after removing all the items, it should start again from Title 1 and Title 2 rather than Title 3 and Title 4 as in picture 3.
My Code:
<div class="append">
<button type="button" id="addRow" class=" btn btn-success float-right">Add Title
</button>
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title[]" class="form-control" id="row_0"
placeholder="Enter Product Title">
<a class="btn btn-danger remove" onclick="deleteTitleRow();">X</a>
</div>
</div>
$("#addRow").on('click', function () {
count++;
addTitleRow(count);
});
function addTitleRow(x) {
let addTitle = '<div class="form-group">\n' +
'<label for="title">Title ' + x + '</label>\n' +
'<input type="text" name="title[]" class="form-control" id="row_' + x + '"\n' +
' placeholder="Enter Product Title">\n' +
'<a class="btn btn-danger remove" onclick="deleteTitleRow();">X</a>\n' +
'#if($errors->has('title'))\n' +
' <p class="error alert alert-danger">{{$errors->first('title')}}</p>\n' +
'#endif\n' +
'</div>';
$(".append").append(addTitle);
}
function deleteTitleRow() {
$("body").on("click", "a.remove", function () {
$(this).parents(".form-group").remove();
});
}
You must calculate title count when add and remove events called. Check below code:
$("body").on("click", "button.remove-row", function() {
$(this).parent().remove();
calculateTitles(); // Call when removed row for title count calculation.
});
$("body").on("click", "button.add-row", function() {
let addTitle = '<div class="form-group titles">' +
'<label for="title" class="row-title"></label>' +
'<input type="text" name="title[]" class="form-control" placeholder="Enter Product Title">' +
'<button class="btn btn-danger remove-row">X</button>' +
'</div>';
$(".append").append(addTitle);
calculateTitles(); // Call when added row for title count calculation.
});
function calculateTitles() {
// Find all titles, add label text as 'Title {index}', add one to index because it's starting from zero.
$('.titles').each(function(index, title) {
$(title).find('label.row-title').text('Title ' + (index + 1));
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="append">
<button type="button" class=" btn btn-success float-right add-row">Add Title</button>
<div class="form-group titles">
<label for="title">Title 1</label>
<input type="text" name="title[]" class="form-control" placeholder="Enter Product Title">
<button class="btn btn-danger remove-row">X</button>
</div>
</div>
I am attempting to create an expandable form to create on/off instructions that a user can submit times for in pairings, so my HTML defaults with one pair and the user can use a button to add additional pairs, but when i submit the form angular is only reading the first pairing, can someone point out what I am missing here? Am I appending in the extra fields improperly?
HTML
<div class="timing">
<form class="timingSelect" action="index.html" method="post">
<div class="inputField">
<div class="form-group">
On: <input type="number" ng-model="recipe.on1" value="" step=".1">
</div>
<div class="form-group">
oz: <input type="number" ng-model="recipe.oz1" value="" readonly="readonly">
</div>
<div class="form-group">
Off: <input type="number" ng-model="recipe.off1" value="" step='.1'>
</div>
</div>
<!-- <input type="submit" ng-click="createRecipe(recipe)" value="Generate Recipe"> -->
</form>
<button type="submit" ng-click="createRecipe(recipe)">Submit</button>
</div>
<button type="button" class="next" name="button" ng-click="addColumn()">+</button>
JS:
app.controller('CreateRecipeController', ['$scope', '$location', '$routeParams', 'DashFactory', function($scope, $location, $routeParams, DashFactory){
console.log("entered Create Recipe controller");
var columnCount = 1;
$scope.addColumn = function addColumn(){
columnCount++;
console.log('attempting to create column');
var d = document.createElement("div");
d.className = "inputField";
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");
var d2 = document.createElement("div");
d2.className = "form-group"
var i = document.createElement("input"); //input element, text
i.setAttribute('type',"number");
i.setAttribute('ng-model',"recipe.on"+columnCount);
i.setAttribute('value',"");
var d3 = document.createElement("div");
d3.className = "form-group"
var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"number");
s.setAttribute('ng-model',"recipe.oz"+columnCount);
s.setAttribute('value',"");
s.setAttribute('readonly','readonly')
var d4 = document.createElement("div");
d4.className = "form-group"
var t = document.createElement("input"); //input element, Submit button
t.setAttribute('type',"number");
t.setAttribute('ng-model',"recipe.off"+columnCount);
t.setAttribute('value',"");
d.appendChild(f);
f.appendChild(d2);
f.appendChild(d3);
f.appendChild(d4);
d2.appendChild(i);
d3.appendChild(s);
d4.appendChild(t)
document.getElementsByClassName('timingSelect')[0].appendChild(d);
}
$scope.createRecipe = function(recipe){
console.log('recieved recipe data', recipe);
DashFactory.createRecipe(recipe)
}
}
]);
Great - Just move all your buttons inside your form tag like the code below
<div class="timing">
<form class="timingSelect" action="index.html" method="post">
<div class="inputField">
<div class="form-group">
On: <input type="number" ng-model="recipe.on1" value="" step=".1">
</div>
<div class="form-group">
oz: <input type="number" ng-model="recipe.oz1" value="" readonly="readonly">
</div>
<div class="form-group">
Off: <input type="number" ng-model="recipe.off1" value="" step='.1'>
</div>
</div>
<button type="submit" ng-click="createRecipe(recipe)">Submit</button>
<button type="button" class="next" name="button" ng-click="addColumn()">+</button>
</form>
</div>
And don't add another form when you append a new input field just add all your new inputs inside the existing form and try to submit it again - This might work
Thanks - Happy Coding !!
I am currently trying to find a way to extract a value from one textbox and pass that value to a title of a dropdownbox.
Textbox
<div class="input-group">
<input type="text" id="sDate" class="form-control" placeholder="" aria-describedby="basic-addon1" readonly="readonly" value =" {{calendar | date:'dd MMM y'}} " #sDate/>
</div>
Dropdown
<button class="btn btn-outline-primary" id="dropdownMenu1" ngbDropdownToggle>
{{title}}<img src="images/dateicon.png">
</button>
Typescript
this.title = (<HTMLInputElement>document.getElementById("sDate" + this.id)).value;
I created an application which sends json data to a google form with ajax. My problem is that I'm not able to send all the data I wish, which means that I'm not able to send some content inside a textarea and a select tag. I'm working with the url, unfortunately I had to use some hidden content in order to move some data to from a page to another page, if I wont' I'm not able to get some data.
Here is my code: The javascript:
$( document ).ready(function() {
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function postContactToGoogle() {
var full_name = getParameterByName('full_name');
var home_address = getParameterByName('home_address');
var email = getParameterByName('email');
var phone_number = getParameterByName('phone_number');
var condition = getParameterByName('condition');
var extra = getParameterByName('extra');
var when_to_sell = getParameterByName('when_to_sell');
console.log(full_name, home_address, email, phone_number, condition, extra, when_to_sell);
var google_url = "https://docs.google.com/forms/d/e/1FAIpQLSdqMQL_dzhdFSVpuVtlcfIa4xHe9DNP8yozB1ncAB2e_byBJQ/formResponse";
if (full_name !== "" || email !== "" || phone_number !== "" || home_address !== "" || condition !== "" || extra !== "" || when_to_sell !== "")
{
$.ajax({
url: google_url,
data: {
"entry.515947838": full_name,
"entry.190795925": email,
"entry.1306603567": phone_number,
"entry.1032461271": home_address,
"entry.100378601": condition,
"entry.637314286": extra,
"entry.608122467": when_to_sell
},
type: "POST",
dataType: "json",
statusCode: {
0: function () {
},
200: function () {
}
}
});
}
}
postContactToGoogle();
});
And here's the html which is creating me a lot of problem: This is a page when I'm collecting some data that I will later send with the ajax
<form action="step-3.html" id="form-step2">
<div class="form-control full-width">
<label for="condition">What conditions is the property in?</label>
<select id="condition" name="condition">
<option value="Good">Good</option>
<option value="Fair">Fair</option>
<option value="Bad">Bad</option>
</select>
</div>
<div class="form-control full-width">
<label for="extra">Any updates, amenities or extras?</label>
<textarea name="extra" id="extra" placeholder="e.g: recent reno, pool etc." rows="3"></textarea>
</div>
<div class="form-control full-width">
<label for="when_to_sell">When do you want to sell?</label>
<select id="when_to_sell" name="when_to_sell">
<option value="Immediately">Immediately</option>
<option value="Next 1-3 months">Next 1-3 months</option>
<option value="Next 3-6 months">Next 3-6 months</option>
</select>
</div>
<div class="form-control full-width">
<label>Are you currently working with a Real Estate Agent?*</label>
<input checked="checked" id="working_for_real_estate_agent_no" name="working_for_real_estate_agent" type="radio" />
<label class="radio-label" for="working_for_real_estate_agent_no">No</label>
<input id="working_for_real_estate_agent_yes" name="working_for_real_estate_agent" type="radio" />
<label class="radio-label" for="working_for_real_estate_agent_yes">Yes, I am working with a Realtor.</label>
<div class="asteriks">*We ask this to ensure there is no conflict of interest.</div>
</div>
<div class="form-control full-width">
<input id="home_address" name="home_address" type="hidden" />
<div class="home-evaluation-option-container">
<div class="home-evaluation-option">
<input id="exact-market-value" name="exact-market-value" type="button" value="Send Property Details" />
</div>
</div>
</div>
</form>
And here is the hack that I tried, which unfortunately works only with the input tag:
<form action="step-4.html" id="form-step2">
<div class="form-control full-width">
<input name="full_name" placeholder="Full Name" type="input" data-validation="length alphanumeric" data-validation-length="min1" data-validation-error-msg="Please, insert your name" />
</div>
<div class="form-control full-width">
<input id="email" name="email" placeholder="Email" type="input" data-validation="email" />
</div>
<div class="form-control full-width">
<input id="phone_number" name="phone_number" placeholder="Phone Number" type="input" name="home-phone" data-validation="number" />
</div>
<input id="home_address" name="home_address" type="hidden" class="hide" />
<select id="condition" name="condition" class="hide">
<option value="Good">Good</option>
<option value="Fair">Fair</option>
<option value="Bad">Bad</option>
</select>
<textarea name="extra" id="extra" class="hide"></textarea>
<select id="when_to_sell" name="when_to_sell" class="hide">
<option value="Immediately">Immediately</option>
<option value="Next 1-3 months">Next 1-3 months</option>
<option value="Next 3-6 months">Next 3-6 months</option>
</select>
<div class="form-control submit-button">
<input name="submit" type="submit" value="Generate Report" />
</div>
</form>
Any thought in order to collect data from a select or a text area?
Here is the page where you can find the code in action:
http://homeworth.online/hockinghomesteam/
Here is an example of how you can get the value out of a textarea element:
<!DOCTYPE html>
<html>
<body>
<textarea id="idExtra_Info">
Some content
</textarea>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var x = document.getElementById("idExtra_Info").value;
alert(x);
}
</script>
</body>
</html>
html
<form name="eventInformation" id="eventInformation"><label class="required"> Occurence Date </label>
<p class="input-group">
<input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="formData_EventDetails.eventOccurDate"
is-open="popup[0].opened" datepicker-options="dateOptions" close-text="Close" required/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open(0)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
<div class="col-sm-2 pull-right">
<button class="btn btn-block btn-primary ebtn"
ng-disabled="eventInformation.$invalid"
ng-click="submit()">Save</button>
</div>
</form>
javascript
$scope.format = 'yyyy/MM/dd';
$scope.dateOptions = {
formatYear : 'yy',
startingDay : 1
};
$scope.popup = [];
for (i = 0; i < 10; i++) {
$scope.popup[i] = {
opened : false
};
}
$scope.open = function(i) {
$scope.popup[i].opened = true;
};
I have bootstrap datepicker element which i am trying to only accept date as input, but it accepts all kinds of inputs i.e. string, numbers etc. How can i limit so it only takes date as input
Since it's a input text box, You can't restrict the user to enter invalid characters(default behavior).
But you can stop the model update operation, when the user provides wrong date (for that you need to setup following attributes on your input element)
ng-model-options="{allowInvalid: false}"