Solved

Remove alert and dropdownlist value does not get selected

Posted on 2013-10-30
3
448 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
[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 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to use string functions to get particular string 3 49
Access-Control-Allow-Origin 4 39
Time Picker 3 20
history of pages 7 47
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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)

738 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