0

Using jquery 1.7.1, I'm using json to populate the dropdown. The code works just great and is is stored in a function but called upon document.ready(). I hope someone finds it useful, BUT:

proxy.invoke("fetchCHAA", {},
        function(result) {

            if (result == null) {
                alert("Possible error in fetching list of CHAAs");
                return;
            }
            else {

                var optionsValues = '<select>';
                optionsValues += '<option value=""></option>';
                $.each(result, function() {
                    optionsValues += '<option value="' + this.chaa_id + '">' + this.chaa_name + '</option>';
                });
                optionsValues += '</select>';
                var options = $('#optionsChaa');

                options.replaceWith(optionsValues);


            }


        }, onPageError);

As I said, the problem lies with .change event or even attempting to get the value of the selected item. For example, we know that

var id = $("#optionsChaa").val();
// should bring back the chaa_id

Or:

var text = $("#optionsChaa").text();
// should bring back the chaa name

The problem is that bringing back the ID or TEXT through .val() is always undefined. I've noticed that others seem to have the same problem yet seemingly unable to address it.

I even added the change event to the optionsValue object hoping that it will work after the DOM is loaded but that does not seem to work as well.

I don't want to hard code the list of options on the page but it looks like I might have to.

1
  • 6
    Aren't you erasing the select's ID with your replaceWith call, meaning jQuery can no longer find it with that selector? Commented Feb 15, 2012 at 17:32

1 Answer 1

2

Try changing var optionsValues = '<select>'; to var optionsValues = '<select id="optionsChaa">';

The ID is getting lost I think.

edit @David M beat me to it, that's the answer.

Sign up to request clarification or add additional context in comments.

3 Comments

David M and Jonathan. Here's a baseball bat for each one of you. Go gentle, okay? :-) Thanks!
Ha, I don't want to fight, can I gift some rep?
The code works great - dyanmically loading the select drop down but the kicker is when using .change the result is undefined....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.