views:

17

answers:

1

Trying to load data into a drop down list when a user selects one of two radio buttons. the bookGenres.xml file is in the same directory as my script. I'm out of ideas.

XML:

<?xml version="1.0" encoding="utf-8" ?>
<Genres>
  <Fiction>
    <book>Sci-Fi</book>
    <book>Fantasy</book>
    <book>Horror</book>
    <book>Romance</book>
    <book>Detective</book>
  </Fiction>
  <NonFiction>
    <book>Autobiography</book>
    <book>Philosophy</book>
    <book>Cooking</book>
    <book>Historic</book>
    <book>Teaching</book>
  </NonFiction>
</Genres>

JQuery:

$('#rblGenre').click( function() {
        $('#ddlSpecificGenre > option').remove();

        $.ajax({
            type: 'GET',
            url: 'bookGenres.xml',
            dataType: 'xml',
            success: function( xml ) {
                $(xml).find( $(this).val() ).each( function() {
                    var subgenre = $(this).find('book').text();
                    $('#ddlSpecificGenre').append( "<option>" + subgenre + "</option>" );
                });
            },
            error: function() { alert( "WTF" ); }
        });
    });
+1  A: 

Justen,

The most likely issue is that the bookgenres.xml might be in the same folder as the script, but is it in the same folder that the html page is that the jQuery is getting renered on?

Ryk
No it isn't. Needs to be?
Justen
Yes, else you need to give it a relative path to it. like '../JScripts/bookgenres.xml'
Ryk
Give this relative path where? Previously, I had the XML file in its own folder and gave the relative path in the $.ajax() call. What tag do I use and do I place that in the html file?
Justen
Alright, got a successful ajax call, just need to work out a bug with my jquery to add the elements to the ddl. I moved the xml file to the root directory, and changed the ajax url to the relative path. But still, how would I had a relative path so the html page knows about it without me putting it in the same dir?
Justen
if your xml file is in the folder "/files/xml_files/bookgenres.xml" and your html page in "/subfolder1/index.html" then the relative path would be "../files/xml_files/bookgenres.xml" and since the html file is where it is, the relative path would always be the same.The "../" notation means "go up one folder level". If you render the jQuery on files in different folders, you will just have to keep track of it. A notation that might work is also the "~/" notation that means root. The best advice is to get Firefox and install firebug to debug these.
Ryk