1

I have created a dropdown menu that pulls from an xml file. However, I would like the "value" of each option in the xml to also be visible in the dropdown option. So with the below xml code the dropdown text would look like this:

 Doctor 1
 Dentist 2
 Vet 3

etc.

How do I pull the value as well?

    //XML
    <ps>
    <specialty value="1">Doctor</specialty>
    <specialty value="2">Dentist</specialty>
    <specialty value="3">Vet</specialty>
    <ps>

    $(document).ready(function () {
    var ps_data;

    // Loading xml
      $.get('test.xml', function (data) {
        ps_data = data;
        var ps = $('#special');
        $('specialty', ps_data).each(function () {
            $('<option />', {
                text: $(this).text(),
                value: $(this).attr('index')
            }).appendTo(ps);
          });     
        }, 'xml');
       });


    //HTML
    <div>
       <select name="Count_1" class="special" id="special">
          <option value="">Select Specialty</option>
       </select>

    </div>

1 Answer 1

1

All you should have to do is update your text line to the following:

text: $(this).text()+ " " + $(this).attr('value'),

Have you tried that?

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

Comments

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.