javascript - How can I get an array element out of an array in the value of a form element? -
i have html form element value set array. want 1 of these values out of array via jquery. how done? here code. maybe can speak if explanation wasn't clear. thank you!
var lat = 0; var long = 0; //gets latitude , longitude when user clicks on place on map map.on('click', function(e) { lat = e.latlng.lat; long = e.latlng.lng; //store lat , long in array var latlong = [lat, long]; //here setting value of element in form array of lat , long document.getelement(".<?= $this->getname() ?>").set("value", latlong); var getlatlong = jquery(".<?= $this->getname() ?>").val(); console.log("retrieved: " + getlatlong); //it saves , retrieves properly. //the problem here //the problem here var lat = $(".<?= $this->getname() ?>").val()[0]; var long = $(".<?= $this->getname() ?>").val()[1]; //the problem here //the problem here });
this runs fine not reuslt want. if try using of lines of code below
//the problem here var lat = $(".<?= $this->getname() ?>").val()[0]; //returns 9 var long = $(".<?= $this->getname() ?>").val()[1]; //returns 4 var long = $(".<?= $this->getname() ?>").val()[3]; //returns 2 var long = $(".<?= $this->getname() ?>").val()[4]; //returns 3 //the problem here
to either latitude or longitude "ie: 94.2323, 12.9932"
the index [0] returns 9, index [1] returns 4, index [3] returns 2
as if 94.2323 array instead of lat , long array. want [0] return 94.2323, , [1] return 12.9932.
i tried explaining , formatting question best could. thank you.
you can use json.stringify()
convert array
json
format
document.getelement(".<?= $this->getname() ?>") .set("value", json.stringify(latlong));
json.parse()
convert json
formatted string
array
var getlatlong = json.parse(jquery(".<?= $this->getname() ?>").val()); console.log(getlatlong[0], getlatlong[1]); // stuff
Comments
Post a Comment