4

I have this xml document:

<?xml version="1.0" encoding="utf-8"?>
<root>
<chapter>
<lesson>message 1</lesson> 
<lesson>message 2</lesson>
<lesson>message 3</lesson>
<lesson>message 4</lesson>
<lesson>message 5</lesson>
<lesson>message 6</lesson>
<lesson>message 7</lesson>
<lesson>message 8</lesson>
<lesson>message 9</lesson>
<lesson>message 10</lesson>
<lesson>message 11</lesson>
</chapter>
</root>

This is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>  
<script type="text/javascript" src="jquery.js"></script>  
<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "numbers.xml",
dataType: "xml",
success: parseXml
});
function parseXml(xml) {

  $(xml).find("chapter").each(function()  {

      $(this).find("lesson").each(function()  {


         $("#dropdownlist").val($(this).text());

         $("select").change(function () {          
 var str = "";          
 $("select option:selected").each(function () {                
str += $(this).val().text() + " ";              
});         
$("#dropdownlist").val(str);        
})        
.change();

      });
      });
}



});
</script>  
</head>

<body>  
<div>
<form id="myform" name="form1" action="" method="get">
<input style="border-style: inset" maxlength="70" size="90" type="text" id="dropdownlist" />
</form>
</div>
<table>
<p style="font-family: 'Monotype Corsiva'" align="right">
    chapter
    <select style="width: 100px" name="lessons" id="dropdownlist">
        <option>lesson_1</option>
        <option>lesson_2</option>
        <option>lesson_3</option>
        <option>lesson_4</option>
        <option>lesson_5</option>
        <option>lesson_6</option>
        <option>lesson_7</option>
        <option>lesson_8</option>
        <option>lesson_9</option>
        <option>lesson_10</option>
        <option>lesson_11</option>
    </select>
</p>
</table>
</body>  
</html>  

My problem is that the code stack and show me only the first result from xml parse. When i choose the first choice from dropdown menu everything it is ok, but when i choose the others options stack and show me the first again. Any suggestions?

1 Answer 1

1

Try this code:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "numbers.xml",
        dataType: "xml",
        success: function(response) {
            $('lesson', response).each(function() {
                $("#dropdownlist").append($('<option />').text($(this).text()));
            });
        }
    });

    $("select").change(function() {
        var str = '';

        $(this).find(":selected").each(function() {
            str += $(this).text() +' ';
        });

        $("#dropdownlist").val(str);
    }).change();
});

You were attaching a change() trigger to your element every time you looped.

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

1 Comment

i want to take data from xml document. this code show the dropdown options (lesson_1,lesson_2,lesson_3...) e.g when i choose the first choice from dropdown list i want to show me "message 1" from xml file

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.