aldrin
aldrin

Reputation: 71

Inserting XML source to select using javascript

I have an XML source that was created like this:

<?xml version="1.0"?>
<response>
<fnotes><option  value="">Select available time</option>
<option  value="0600">06:00 AM</option>
<option  value="0615">06:15 AM</option>
<option  value="0630">06:30 AM</option>
<option  value="0645">06:45 AM</option>
</fnotes>
</response>

Then my javascript looks like this:

var select = $('#sappttime');
$(xml).find('fnotes').each(function(){
var title = $(this).find('option').text();
select.append("<option/><option class='ddheader'>"+title+"</option>");
});

I want my select to have 4 values but it's returning as one complete value instead. I was thinking of 2 solutions to populate the select base on the source xml. 1 is the example above, 2 adding the value on my select as is (don't know how to do that as well though). Any help is appreciated. Thank you.

So I have revised this to look like this:

var select = $('#sappttime');
$(xml).find('option').each(function(){
var title = $(this).find('option').text();
select.append("<option class='ddheader'>"+title);

It seems to loop into each record by value is being blank. Basically variable title is returning as empty but iterates through each records.

Upvotes: 0

Views: 36

Answers (1)

Paul
Paul

Reputation: 36339

You don't need to find again. Try this:

var select = $('#sappttime');
$(xml).find('option').each(function(){
// $(this) in here is the option element
var title = $(this).text();
select.append("<option class='ddheader'>"+title);

Upvotes: 1

Related Questions