I have an array in which my Source and Destination are like this:
markers.push({
"Location": "Chicago",
"IsLocation": "Yes"
});
markers.push({
"Location": "Los Angeles",
"IsLocation": "Yes"
});
Now when i will create points with my dynamic textbox then I would like to add those all points in between source and destination.
Scenario 1: first dynamic textbox with input say for Eg: abc
markers[0]:Chicago
markers[1]:abc
marker[2]:Los Angeles.
Scenario 2: second dynamic textbox with input say for Eg: pqr
markers[0]:Chicago
markers[1]:abc
markers[2]:pqr
marker[3]:Los Angeles.
Now when I change the second textbox value that is from pqr to lmn then it creates a new entry in markers array instead of updating pqr to lmn.
You can check in console:
How do I stop that?
// Code goes here
var cnt = 1;
var maxNumberOfTextboxAllowed = 5;
var autocomplete = [];
var markers = [];
markers.push({
"Location": "Chicago",
"IsLocation": "Yes"
});
markers.push({
"Location": "Los Angeles",
"IsLocation": "Yes"
});
function Generatetextbox() {
if (cnt <= maxNumberOfTextboxAllowed) {
var fieldWrapper = $("<div class='fieldwrapper' id='field" + cnt + "'/>");
var fName = $("<input type='text' class='fieldname' id='Txtopt" + cnt + "' name='TxtoptNm" + cnt + "' />");
fieldWrapper.append(fName);
fieldWrapper.append('<br />');
fieldWrapper.append('<br />');
$("#abc").append(fieldWrapper);
var newInput = [];
var newEl = document.getElementById('Txtopt' + cnt);
var txtboxId = 'Txtopt' + cnt;
newInput.push(newEl);
setupAutocomplete(autocomplete, newInput, 0, txtboxId);
cnt = cnt + 1;
} else
alert("Cant create more than 5 textbox")
}
function setupAutocomplete(autocomplete, inputs, i, txtboxId) {
autocomplete.push((txtboxId));
var idx = autocomplete.length - 1;
document.getElementById(autocomplete[idx]).addEventListener("change", function() {
var autoTextbox = [{
"Location": document.getElementById(autocomplete[idx]).value,
"IsLocation": "Yes"
}]
var markerLastIndexData = [{
"Location": markers[markers.length - 1].Location,
"IsLocation": "Yes"
}]
markers.splice(markers.length - 1, 1, autoTextbox);
markers.splice(markers.length, 0, markerLastIndexData);
console.log(markers)
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="abc"></div>
<button onclick="Generatetextbox()" class="btn btn-primary" type="button">Add</button>
