0
votes

My Web Page has two Dropdown list 1) Containing Book Name 2) Containing link Of the Book. The Data is stored in form of XML File something like this:

<?xml version="1.0" encoding="UTF-8"?>
<BookDetail>
<book>
    <bookName>Dollar Bahu</bookName>
    <link>http:\\DollarBahu.in</link>
</book>
<book>
    <bookName>The Lost Symbol</bookName>
    <link>http:\\TheLostSymbol.in</link>
</book>
<book>
    <bookName>Keep The Change</bookName>
    <link>http:\\KeepTheChange.in</link>
</book>
</BookDetail>

Function: window.onload=function() { var xhttp = new XMLHttpRequest(); xhttp.open("GET", "bookDetails.xml", false); xhttp.send(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { var xml = xhttp.responseXML; var books = xml.getElementsByTagName('book'); var book = document.getElementById('book'), link = document.getElementById('link');

                    for (i = 0; i < books.length; i++)
                    {
                        var obook = document.createElement('option'),
                        olink = document.createElement('option'),
                        tbook =   document.createTextNode(books[i].children[0].innerHTML),
                        tlink =   document.createTextNode(books[i].children[1].innerHTML);

                        obook.appendChild(tbook);
                        olink.appendChild(tlink);
                        book.appendChild(obook);
                        link.appendChild(olink);
                        map[tbook] = olink;
                    }
                    var updateLink = function(e)
                    {
                        //map[e.target.value].setAttribute('selected','');
                        //alert(map[e.target.value]);
                        for (var i = 0; i< link.children.length; i++)
                        {
                            link.children[i].removeAttribute('selected');
                        }
                           link.children[this.selectedIndex].setAttribute('selected','');
                    };
                    book.addEventListener('change', updateLink, false);
                }


                //var xmlString = "<BookDetail><book>    <bookName>Dollar Bahu</bookName>    <link>http:\\DollarBahu.in</link></book><book>    <bookName>The Lost Symbol</bookName>    <link>http:\\TheLostSymbol.in</link></book><book>    <bookName>Keep The Change</bookName>    <link>http:\\KeepTheChange.in</link></book></BookDetail>";
                //var xml = ( new window.DOMParser() ).parseFromString(xmlString, "text/xml"),i,
                //map = {};

            }

        }
    </script>

I have successively inserted valus in first dropdown list containing bookName. Now my question is how to get book link in second dropdown list of that particular bookName from XML?

2
What is your expected behavior? If I select Dollar Bahu in the first dropdown, http:\\DollarBahu.in should get selected in the second dropdown? or the second dropdown should only be populated with the associated links? This doesn't make much sense, since there is only one link for every book, right?Federico
Yes,There is only one link associated with each book. It should be If I select a Particular book(eg Dollar Bahu) in the first dropdown.Second dropdown should get selected with its associated link (i.e http:\\DollarBahu.in) I am able to fetch the name of book selected by the user but don't know how to use it to fetch associated link from the XML file.Rng

2 Answers

0
votes

This is the complete working solution:

  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "book.xml");
  xhttp.send();
  xhttp.onreadystatechange = function() {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
        var books = xhttp.responseXML.getElementsByTagName('book');
        var book = document.getElementById('book'),
        link = document.getElementById('link');

        for (i = 0; i < books.length; i++) {
            var obook = document.createElement('option'),
                olink = document.createElement('option'),
                tbook = document.createTextNode(books[i].children[0].innerHTML),
                tlink = document.createTextNode(books[i].children[1].innerHTML);

            obook.appendChild(tbook);
            olink.appendChild(tlink);

            book.appendChild(obook);     
            link.appendChild(olink);      
    }
  }
}

var updateLink = function(e) {
    for (var i = 0; i< link.children.length; i++) {
        link.children[i].removeAttribute('selected');
    }
    link.children[this.selectedIndex].setAttribute('selected','');
};

book.addEventListener('change', updateLink, false);

With this HTML:

<!doctype html>
<html>
<meta charset="utf-8" />
<head>
</head>
<body>
  <select name="book" id="book">
  </select>
  <select name="link" id="link">
  </select>
  <script type="text/javascript" src="main.js"></script>
</body>
</html>

This is book.xml

<?xml version="1.0" encoding="UTF-8"?>
<BookDetail>
<book>
    <bookName>Dollar Bahu</bookName>
    <link>http:\\DollarBahu.in</link>
</book>
<book>
    <bookName>The Lost Symbol</bookName>
    <link>http:\\TheLostSymbol.in</link>
</book>
<book>
    <bookName>Keep The Change</bookName>
    <link>http:\\KeepTheChange.in</link>
</book>
</BookDetail>
1
votes

In the onchange event of the bookName dropdown you can retrieve the selectedIndex property, and then use that value to get the corresponding book link.