Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

populate dropdownlist with JSON and jquery how to load selected state

Posted on 2013-10-29
3
Medium Priority
?
675 Views
Last Modified: 2013-10-29
Hi,
I am relatively new to jquery, JSON and the whole MVC structure.

I am doing a webpage for work and one of the dropdownlist has all US states in it. So I found a JSON file with all state names and abbreviations.. so I have it loading by
$(document).ready(function () {
    'use strict';
    $.getJSON("../../JSON/States.json", function (data) {
        var states = data.States;

        $.each(states, function (id, states) {
            $("#SelectState").append("<option value='" + states.abbreviation + "'>" + states.name + "</option>");
        });

    });   
});

Open in new window

and when I select a state it saves the abbreviation like it should but if I press to go to the Edit page I can't get the selected value to show up on the dropdownlist as selected, it always goes to the first state.

I tried
$(document).ready(function () {
       $("#SelectState").Selected($("#hidState").val());
});

Open in new window

but it did nothing( I tried alert($("#hidState").val()); and it did pop up with the right value.
How can I get the value to be selected.

Thank you in advance for any help
0
Comment
Question by:Niall292
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39610213
try this :
$(document).ready(function () {
       $("#SelectState").val($("#hidState").val());
});

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39610216
or :
$(document).ready(function () {
       $("#SelectState option[value='" + $("#hidState").val() + "']").prop('selected', true);
});

Open in new window

0
 

Author Closing Comment

by:Niall292
ID: 39610236
Thanks Leakim971,

Your first response worked perfectly. I didn't try your second option.

Thanks again

Sorry, I responded a bit quick

I kept the alert in when trying it but when I removed the alert it doesn't change anymore, any idea what might be wrong??
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question