javascript - Not getting proper output when shifting values in array position -


i having 1 array in source , destination this:

markers.push({         "location": "chicago",         "islocation": "yes"             });      markers.push({         "location": "los angeles",         "islocation": "yes"             }); 

now when create points dynamic textbox add points in between source , destination.

scenario 1:1st dynamic textbox input eg:abc

markers[0]:chicago markers[1]:abc marker[2]:los angeles. 

scenario 2:2nd dynamic textbox input eg:pqr

markers[0]:chicago markers[1]:abc markers[2]:pqr marker[3]:los angeles. 

scenario 3:3rd dynamic textbox input eg:lmn

markers[0]:chicago markers[1]:abc markers[2]:pqr markers[3]:lmn marker[4]:los angeles. 

my first position 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 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 getting not proper result.

getting output this:enter image description here

console output coming undefine:enter image description here

expected output shown in scenarios like:

marker[0]:{            location="chicago",             isolcation="yes"           } marker[1]:{            location="abc",             isolcation="yes"           } etc...... 

it's happening because in setupautocomplete function you're assigning array instead of object markers array. remove []'s on lines declare 2 variables going pushed markers array.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -