Solved

Remove alert and dropdownlist value does not get selected

Posted on 2013-10-30
3
447 Views
Last Modified: 2013-10-30
This is a continuation of a question yesterday.
I have a dropdownlist which gets populated by an JSON file and jquery, everything works great but when I enter a record and then go to the edit page to edit it.
I want the dropdown list to have the proper value selected on opening but I cannot get it to work
I got help yesterday which seemed to work perfectly
$(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>");
        });

    });
    alert($("#hidState").val());
        var hidden = $("#hidState").val();      
          $("#SelectState").val(hidden)
});

Open in new window


I put an alert in to make sure the value was coming across and it was and the dropdown list got updated and the proper value was selected (All good) So I removed the alert, but when I did the dropdown list does not get the value selected anymore. When I put in the alert everything works but when I remove it the dropdown list gets populated but the proper value is not selected.

hidState is a hidden field which gets updated by  model.State when page opens (In case that makes a difference)

Thanks again for any help
0
Comment
Question by:Niall292
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39611304
Check this :
$(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>");
        });

        var hidden = $("#hidState").val();      
        $("#SelectState").val(hidden)

    });
});

Open in new window

0
 

Author Comment

by:Niall292
ID: 39611325
Again you saved the day,
Was it just the positioning that was the problem
0
 

Author Closing Comment

by:Niall292
ID: 39611327
You make it look so easy.
Thanks again
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

821 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