tags:

views:

137

answers:

2

I want to populate a DropDown control on page load using AJAX. I have the code and it is working, but I am not following as to which event to use.

A: 

Firstly, what does the ajax callback return? Partial html formatted as json/xml, or straight up HTML?

One way would be to just re-build the select element and iterate and append option elements, then use the replaceWith method to replace the dropdown. If you have an event on the dropdown, you may need to use liveQuery in order for it to "stick". It would help seeing the code you currently have in order for a definitive answer.

meder
Doesn't he ask for the event on which attach his code?
Eineki
+1  A: 

I'm assuming you're not using a JavaScript framework, but this is simple with jQuery.

$(document).ready(function(){ 
    $("#some_div").load("/dropdown.html", function(){
        [any additional code to make it work]
    });
});

I hope I'm understanding your question correctly.

Greg
Can this code be just pasted on the HTML form or anything else is needed? I am not aware of JQuery.
RPK
This code would go between your <script> tags, just like any javascript. In a word, what it does is (1) waits for the HTML document to be loaded (2) fetches a page with AJAX and puts into an element with id "some_div" in this case (3) optionally executes some more javascript to make further use of the page that it just fetched when THAT page is loaded. This is called chaining. I suggest you google for an introductory tutorial to jquery. It's not hard to learn and it makes things like this a lot simpler
Greg