0

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.

2
  • remove <option/> start of text, you are appending Commented Dec 8, 2014 at 15:53
  • So i revised the javascript and added the new script on my original post. It seems to work but having an empty values though. Commented Dec 8, 2014 at 16:14

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. This perfectly works. Just one more thing though, if I wanted to clear the content of the select element before appending. It should be .remove() right?
I think you want .empty(). .remove() removes the parent element as well. So in this case you'll want to do something like select.empty() first, before your xml operation.

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.