1

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:

enter image description here

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>

1
  • your not checking the existing element which is getting changed Commented Aug 6, 2015 at 10:50

1 Answer 1

1

This will help you problem.

// 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 = markers.length - 1;
  markers[idx+1] = markers[idx];
   markers[idx] = {};
  document.getElementById(txtboxId).addEventListener("change", function() {
var autoTextbox = {
  "Location": this.value,
  "IsLocation": "Yes"
};

   //markers.splice(markers.length - 1, 1, autoTextbox);
   markers[idx] = autoTextbox;
   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>

Sign up to request clarification or add additional context in comments.

12 Comments

In your output where is source and destination?
Textbox value should come in between source and destination like i have shown in my scenarios
or simply use this document.getElementById(autocomplete[idx]).addEventListener("change", function() { //markers.splice(markers.length - 1, 1, autoTextbox); markers[idx].Location = this.value; console.log(markers) });
try now after change only we will update the markers
Your output does not contains my source and destination value and this dynamic textbox value should be in between my source and destination value
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.