Populate selectors using json doesn't work as expectedHow do JavaScript closures work?How do I format a Microsoft JSON date?Can comments be used in JSON?How to get the children of the $(this) selector?How can I pretty-print JSON in a shell script?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
What did the controller say during my approach to land (audio clip)?
US entry with tourist visa but past alcohol arrest
Applications of mathematics in clinical setting
Figuring out the frequency components using FFT
Centrifugal force with Newton's third law?
Hiking with a mule or two?
Gas leaking in base of new gas range?
Cheap antenna for new HF HAM
Paradox regarding phase transitions in relativistic systems
Hilbert's hotel, why can't I repeat it infinitely many times?
Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?
Social leper versus social leopard
How do I clean sealant/silicon from a glass mirror?
Writing a letter of recommendation for a mediocre student
Safely hang a mirror that does not have hooks
Repeat elements in list, but the number of times each element is repeated is provided by a separate list
As an employer, can I compel my employees to vote?
Do things made of adamantine rust?
Escape the labyrinth!
What is the fastest way to do Array Table Lookup with an Integer Index?
Can Bless or Bardic Inspiration help a creature from rolling a 1 on a death save?
Is there any reason nowadays to use a neon indicator lamp instead of an LED?
Why are some of the Stunts in The Expanse RPG labelled 'Core'?
Debussy as term for bathroom?
Populate selectors using json doesn't work as expected
How do JavaScript closures work?How do I format a Microsoft JSON date?Can comments be used in JSON?How to get the children of the $(this) selector?How can I pretty-print JSON in a shell script?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to make two selector's populated with JSON. One for states and one for cities. If i choose a state the next selector is supposed to show me the cities that are in that state.
I've made it so far using functions. My state function is working fine, but I'm having troubles with my city selector. It doesn't show anything.
I'm stuck here.
In my scripts.js I have
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states.cities)
listobj += '<option value="'+data.states.cities[i].id+'">'+data.states.cities[i].name+'</option>';
$('#cities').html(listobj);
And in my ready.js where i use the functions I have
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
Here`s the HTML
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
javascript html json select jquery-selectors
add a comment
|
I am trying to make two selector's populated with JSON. One for states and one for cities. If i choose a state the next selector is supposed to show me the cities that are in that state.
I've made it so far using functions. My state function is working fine, but I'm having troubles with my city selector. It doesn't show anything.
I'm stuck here.
In my scripts.js I have
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states.cities)
listobj += '<option value="'+data.states.cities[i].id+'">'+data.states.cities[i].name+'</option>';
$('#cities').html(listobj);
And in my ready.js where i use the functions I have
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
Here`s the HTML
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
javascript html json select jquery-selectors
1
both ids are "states" in your html, they should be states and cities i guess
– chriss
Mar 28 at 15:06
In my code is fine. I copied twice the same line by mistake
– T x
Mar 29 at 7:14
add a comment
|
I am trying to make two selector's populated with JSON. One for states and one for cities. If i choose a state the next selector is supposed to show me the cities that are in that state.
I've made it so far using functions. My state function is working fine, but I'm having troubles with my city selector. It doesn't show anything.
I'm stuck here.
In my scripts.js I have
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states.cities)
listobj += '<option value="'+data.states.cities[i].id+'">'+data.states.cities[i].name+'</option>';
$('#cities').html(listobj);
And in my ready.js where i use the functions I have
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
Here`s the HTML
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
javascript html json select jquery-selectors
I am trying to make two selector's populated with JSON. One for states and one for cities. If i choose a state the next selector is supposed to show me the cities that are in that state.
I've made it so far using functions. My state function is working fine, but I'm having troubles with my city selector. It doesn't show anything.
I'm stuck here.
In my scripts.js I have
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states.cities)
listobj += '<option value="'+data.states.cities[i].id+'">'+data.states.cities[i].name+'</option>';
$('#cities').html(listobj);
And in my ready.js where i use the functions I have
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
Here`s the HTML
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
javascript html json select jquery-selectors
javascript html json select jquery-selectors
edited Mar 29 at 7:14
T x
asked Mar 28 at 15:03
T xT x
175 bronze badges
175 bronze badges
1
both ids are "states" in your html, they should be states and cities i guess
– chriss
Mar 28 at 15:06
In my code is fine. I copied twice the same line by mistake
– T x
Mar 29 at 7:14
add a comment
|
1
both ids are "states" in your html, they should be states and cities i guess
– chriss
Mar 28 at 15:06
In my code is fine. I copied twice the same line by mistake
– T x
Mar 29 at 7:14
1
1
both ids are "states" in your html, they should be states and cities i guess
– chriss
Mar 28 at 15:06
both ids are "states" in your html, they should be states and cities i guess
– chriss
Mar 28 at 15:06
In my code is fine. I copied twice the same line by mistake
– T x
Mar 29 at 7:14
In my code is fine. I copied twice the same line by mistake
– T x
Mar 29 at 7:14
add a comment
|
1 Answer
1
active
oldest
votes
The issue is that you can't iterate through the cities like that, with for(var i in data.states.cities)...
. You need to iterate through just the cities belonging to the selected state.
Here's a working example.
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states)
if (data.states[i].id == $("#states").val())
//this is the selected state, let's get their cities
for(var j in data.states[i].cities)
listobj += '<option value="'+data.states[i].cities[j].id+'">'+data.states[i].cities[j].name+'</option>';
$('#cities').html(listobj);
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
You would probably want to do something like call the populateCities
as a callback when you're done with populateState
on document.ready
, since it comes up with the First state already selected, but doesn't populate its cities. But at least this will get you past your issue.
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55400867%2fpopulate-selectors-using-json-doesnt-work-as-expected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue is that you can't iterate through the cities like that, with for(var i in data.states.cities)...
. You need to iterate through just the cities belonging to the selected state.
Here's a working example.
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states)
if (data.states[i].id == $("#states").val())
//this is the selected state, let's get their cities
for(var j in data.states[i].cities)
listobj += '<option value="'+data.states[i].cities[j].id+'">'+data.states[i].cities[j].name+'</option>';
$('#cities').html(listobj);
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
You would probably want to do something like call the populateCities
as a callback when you're done with populateState
on document.ready
, since it comes up with the First state already selected, but doesn't populate its cities. But at least this will get you past your issue.
add a comment
|
The issue is that you can't iterate through the cities like that, with for(var i in data.states.cities)...
. You need to iterate through just the cities belonging to the selected state.
Here's a working example.
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states)
if (data.states[i].id == $("#states").val())
//this is the selected state, let's get their cities
for(var j in data.states[i].cities)
listobj += '<option value="'+data.states[i].cities[j].id+'">'+data.states[i].cities[j].name+'</option>';
$('#cities').html(listobj);
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
You would probably want to do something like call the populateCities
as a callback when you're done with populateState
on document.ready
, since it comes up with the First state already selected, but doesn't populate its cities. But at least this will get you past your issue.
add a comment
|
The issue is that you can't iterate through the cities like that, with for(var i in data.states.cities)...
. You need to iterate through just the cities belonging to the selected state.
Here's a working example.
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states)
if (data.states[i].id == $("#states").val())
//this is the selected state, let's get their cities
for(var j in data.states[i].cities)
listobj += '<option value="'+data.states[i].cities[j].id+'">'+data.states[i].cities[j].name+'</option>';
$('#cities').html(listobj);
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
You would probably want to do something like call the populateCities
as a callback when you're done with populateState
on document.ready
, since it comes up with the First state already selected, but doesn't populate its cities. But at least this will get you past your issue.
The issue is that you can't iterate through the cities like that, with for(var i in data.states.cities)...
. You need to iterate through just the cities belonging to the selected state.
Here's a working example.
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states)
if (data.states[i].id == $("#states").val())
//this is the selected state, let's get their cities
for(var j in data.states[i].cities)
listobj += '<option value="'+data.states[i].cities[j].id+'">'+data.states[i].cities[j].name+'</option>';
$('#cities').html(listobj);
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
You would probably want to do something like call the populateCities
as a callback when you're done with populateState
on document.ready
, since it comes up with the First state already selected, but doesn't populate its cities. But at least this will get you past your issue.
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states)
if (data.states[i].id == $("#states").val())
//this is the selected state, let's get their cities
for(var j in data.states[i].cities)
listobj += '<option value="'+data.states[i].cities[j].id+'">'+data.states[i].cities[j].name+'</option>';
$('#cities').html(listobj);
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
function populateState(data)
var listState = "";
for(var i in data.states)
listState += '<option value="'+data.states[i].id+'">'+data.states[i].name+'</option>';
$('#states').html(listState);
function populateCities(data)
var listobj = "";
for(var i in data.states)
if (data.states[i].id == $("#states").val())
//this is the selected state, let's get their cities
for(var j in data.states[i].cities)
listobj += '<option value="'+data.states[i].cities[j].id+'">'+data.states[i].cities[j].name+'</option>';
$('#cities').html(listobj);
var dataJson =
"states": [
"name": "First state",
"id": "1",
"cities": [
"name": "city1",
"id": "cos"
,
"name": "city2",
"id": "mio"
,
"name": "city3",
"id": "top"
]
,
"name": "Second state",
"id": "2",
"cities": [
"name": "city4",
"id": "or"
,
"name": "city5",
"id": "st"
,
"name": "city6",
"id": "bs"
]
,
]
;
$(document).ready(function()
populateState(dataJson);
$("#states").change(function ()
populateCities(dataJson);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<select id="states" >
<option value="000">-Select State-</option>
</select>
<select id="cities" >
<option value="000">-Select Cities-</option>
</select>
answered Apr 18 at 22:12
Reverend PeteReverend Pete
4773 silver badges8 bronze badges
4773 silver badges8 bronze badges
add a comment
|
add a comment
|
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55400867%2fpopulate-selectors-using-json-doesnt-work-as-expected%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
both ids are "states" in your html, they should be states and cities i guess
– chriss
Mar 28 at 15:06
In my code is fine. I copied twice the same line by mistake
– T x
Mar 29 at 7:14