use Ajax and JavaScript to create dynamic links from the results

772 Views Asked by At

HTML contains two div make different ajax calls to both the divs,where div1 is the result of html form and div2 should be the result of onclick on div1

html

<html lang="en">
  <head>
    <title>Display Movie Information</title>
    <meta charset="utf-8"/>
    <script type="text/javascript" src="movies.js"></script>
  </head>
  <body onload="initialize();">
  <tr><td>
    <form>
       <label>Movie title: <input type="text" id="form-input"/></label>
       <input type="button" onclick="sendRequest();" value="Display Info"/>
    </form>
    <div id="output">&nbsp;</div></td>
    <td><div id="right_panel">Display Dynamic results here</div></td>
  </body>
</html>

I have a movies.js file which using ajax gets the result

function initialize () {
}

function sendRequest () {
var xhr = new XMLHttpRequest();
var query = encodeURI(document.getElementById("form-input").value);
xhr.open("GET", "proxy.php?method=/3/search/movie&query=" + query);
xhr.setRequestHeader("Accept","application/json");
xhr.onreadystatechange = function () {
   if (this.readyState == 4) {
      var json = JSON.parse(this.responseText);
      var str = JSON.stringify(json,undefined,2);

      //customized code
      console.log(json.results.length);
      var i=0;
      var movie_title,release_date;
      var movies = new Array();
      while(i<json.results.length)
      {
        movie_title = json.results[i].title;
        release_date = json.results[i].release_date;

        movies[i] = "<li>"+movie_title+"<br/>"+release_date+"<br/></li>";
        i++;
      }       

      document.getElementById("output").innerHTML = "<ul>" + movies + "    </ul>";
       }
   };
   var movie_detail = new XMLHttpRequest();
   var movie_credit = new XMLHttpRequest();
   xhr.send(null);
}

This code displays the title and the release date of the movies(result of onclick on the button)

Q: I want to make the movie title an hyperlink that could make a call like www.imdb.com/(movie_title) and display the result in the second div tag named "right_panel" using only ajax and javascript and no jquery.

0

There are 0 best solutions below