2

I receive this POST answer :

cityname=&placeid=CA%7C2%7C46.8608225%7C-71.3508909&ManualCity=&NewProfileID=20ce4057-cd43-3247-f17f-9f5a65d62bd6&TicketID=293fea0a-10aa-515a-e3df-208428a5eef5

in clear :

cityname:
placeid:CA|2|46.8608225|-71.3508909
ManualCity:
NewProfileID:20ce4057-cd43-3247-f17f-9f5a65d62bd6
TicketID:293fea0a-10aa-515a-e3df-208428a5eef5

I'm OK to work fine with cityname + ManualCity (if is set) and NewProfileID and TicketID. My problem is placeid !

I need to split each part separate with | into different hidden input with jQuery / javascript

like this :

<input type="hidden" id="CountryCode" name="CountryCode" value="" />
<input type="hidden" id="CityCode" name="CityCode" value="" />
<input type="hidden" id="Latitude" name="Latitude" value="" />
<input type="hidden" id="Longitude" name="Longitude" value="" />

CA go to CountryCode 2 go to CityCode 46.8608225 go to Latitude and -71.3508909 go to Longitude

If can separate each part into separate variable i can use $("#CountryCode").attrib("value", varCountryCode);

Thank you for your help !

LiTHiUM2525
  • 297
  • 3
  • 6
  • 19

3 Answers3

4
var str = "CA|2|46.8608225|-71.3508909";
var arr = str.split("|");
if (arr.length === 4) {
    var CountryCode = arr[0];
    var CityCode = arr[1];
    var Latitude = arr[2];
    var Longitude = arr[3];
}
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
1

With this you can get n numbers of params very easily.

var getUrlParameter = function getUrlParameter(sParam) {
 var sPageURL = decodeURIComponent(window.location.search.substring(1)),
    sURLVariables = sPageURL.split('&'),
    sParameterName,
    i;

  for (i = 0; i < sURLVariables.length; i++) {
    sParameterName = sURLVariables[i].split('=');

    if (sParameterName[0] === sParam) {
        return sParameterName[1] === undefined ? true : sParameterName[1];
    }
  }
};

And this is how you can use this function assuming the URL is,

http://dummy.com/?technology=jquery&blog=jquerybyexample.

var tech = getUrlParameter('technology');
var blog = getUrlParameter('blog');

Get url parameter jquery Or How to Get Query String Values In js

Community
  • 1
  • 1
0

This explains it quite well. You would end up with an array

http://www.w3schools.com/jsref/jsref_split.asp

Perhaps you could add validation to ensure there is always going to be data in each array position so it will still work and always have 4 items.

     documentGetElementById('CountryCode').value = arr[0];
     documentGetElementById('CityCode').value = arr[1];
     documentGetElementById('Latitude').value = arr[2];
     documentGetElementById('Longitude').value = arr[3];

in JavaScript will put the split values into your hidden inputs ready for submission.

Steve
  • 808
  • 1
  • 9
  • 14