1

I am having 1 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:1st dynamic textbox with input say for Eg:abc

markers[0]:Chicago
markers[1]:abc
marker[2]:Los Angeles.

Scenario 2:2nd dynamic textbox with input say for Eg:pqr

markers[0]:Chicago
markers[1]:abc
markers[2]:pqr
marker[3]:Los Angeles.

Scenario 3:3rd dynamic textbox with input say for Eg:lmn

markers[0]:Chicago
markers[1]:abc
markers[2]:pqr
markers[3]:lmn
marker[4]:Los Angeles.

My first position will be fixed.

Code:

// 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() {
    alert(document.getElementById(autocomplete[idx]).value);
    var autoTextbox = [{
      "Location": document.getElementById(autocomplete[idx]).value,
      "IsLocation": "Yes"
    }]

    var markerLastIndexData = [{
      "Location": markers[markers.length - 1].Location,
      "IsLocation": "Yes"
    }]

    markers[markers.length - 1] = autoTextbox;
    markers[markers.length] = 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>

You can check in console.I am getting not proper result.

Getting output like this:enter image description here

Console output coming undefine:enter image description here

Expected Output are shown in my scenarios like:

marker[0]:{ 
          Location="Chicago",  
          Isolcation="Yes"
          }
marker[1]:{ 
          Location="abc",  
          Isolcation="Yes"
          }
etc......

1 Answer 1

2

It's happening because in the setupAutocomplete function you're assigning an Array instead of an Object to the markers Array. Just remove the []'s on the lines where you the declare the two variables that are going to be pushed to the markers array.

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

1 Comment

Thank you so much.This silly mistake has taken my 2 to 3 hours to solve this.Thanks a ton

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.