0

how to print the json data in html dom?

i have movie json and i am append to select tag and i want to append the movie name to the anchor tag when selecting the movie...

below is my html

<div class="UserData">
            <h1><a href="moviebooking.html">MyMovie-Ticket-Booking</a></h1>
            <select class="selectCity" id="selectCity">
                <option value="City">Select City</option>
                <option value="Bengaluru">Bengaluru</option>
                <option value="Hyderabad">Hyderabad</option>
                <option value="Guntur">Guntur</option>
                <option value="Ongole">Ongole</option>
            </select>
            <span id="welcome"> </span>
        </div>

below is js

$(document).ready(function() {
  var cityData = [{
      cityName: 'Bengaluru',
      value: "Bengaluru",
      data: [{
          movieName: 'ABC',
          theaterName: 'Tulsi Theatre'
        },
        {
          movieName: 'DEF',
          theaterName: 'PVR'
        },
        {
          movieName: 'GHI',
          theaterName: 'Srinivasa Theatre'
        }
      ]
    },


    {
      cityName: 'Hyderabad',
      value: "Hyderabad",
      data: [{
          movieName: '123',
          theaterName: 'Theatre1'
        },
        {
          movieName: '456',
          theaterName: 'PVR2'
        },
        {
          movieName: '789',
          theaterName: 'Theatre3'
        }
      ]
    },

    {
      cityName: 'Guntur',
      value: "Guntur",
      data: [{
          movieName: 'ABC1',
          theaterName: 'Theatre4'
        },
        {
          movieName: 'DEF2',
          theaterName: 'PVR3'
        },
        {
          movieName: 'GHI3',
          theaterName: 'Theatre5'
        }
      ]
    },

    {
      cityName: 'Ongole',
      value: "Ongole",
      data: 'currently not available'
    }
  ];
    $("#selectCity").on('change', function() {
    var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
    var locationString = '';
    var locationString2 = '';
    console.log(locations)
    $.each(locations, function(i, item) {

        console.log(JSON.stringify(item));
        locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
        locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
    });
    $('#secondselectbox').html(locationString);
    $('#thirdselectbox').html(locationString2);
});

});

How to append the theater name and movie name after selecting the movie name in select tag ....

and how to filter the movies and theater separately .....

1
  • Where do you get stuck? What have you tried to do so far? In the meantime it seems you have not started yet. Try to solve the challenge alone, and when you get stuck, and you will not find any other answer in SO, ask a specific question Commented Dec 17, 2017 at 13:35

1 Answer 1

2

You do not have any select with #secondselectbox and #thirdselectbox in your html. Still you are trying to put html in those. Add two select with those id in your html.

$(document).ready(function() {
  var cityData = [{
      cityName: 'Bengaluru',
      value: "Bengaluru",
      data: [{
          movieName: 'ABC',
          theaterName: 'Tulsi Theatre'
        },
        {
          movieName: 'DEF',
          theaterName: 'PVR'
        },
        {
          movieName: 'GHI',
          theaterName: 'Srinivasa Theatre'
        }
      ]
    },


    {
      cityName: 'Hyderabad',
      value: "Hyderabad",
      data: [{
          movieName: '123',
          theaterName: 'Theatre1'
        },
        {
          movieName: '456',
          theaterName: 'PVR2'
        },
        {
          movieName: '789',
          theaterName: 'Theatre3'
        }
      ]
    },

    {
      cityName: 'Guntur',
      value: "Guntur",
      data: [{
          movieName: 'ABC1',
          theaterName: 'Theatre4'
        },
        {
          movieName: 'DEF2',
          theaterName: 'PVR3'
        },
        {
          movieName: 'GHI3',
          theaterName: 'Theatre5'
        }
      ]
    },

    {
      cityName: 'Ongole',
      value: "Ongole",
      data: []
    }
  ];
    $("#selectCity").on('change', function() {
    var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
    var locationString = '';
    var locationString2 = '';
    $.each(locations, function(i, item) {
        locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
        locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
    });
    $('#secondselectbox').html(locationString);
    $('#thirdselectbox').html(locationString2);
});
  $("#thirdselectbox").on("change", function(){
    $("span#selectedMovie").text($(this).val());
  });
  $("#secondselectbox").on("change", function(){
    $("span#selectedTheater").text($(this).val());
  });

  $('#theaterButton').click(function(){
    var arrTheater = [];
    cityData.forEach(function(theater, i){
      var allTheaters = theater.data.map(t => t.theaterName);
      arrTheater = arrTheater.concat(allTheaters);
    });
    $('#container').html('<b>Theaters: </b><br/>' + arrTheater.join(', '));
  });
  
  $('#movieButton').click(function(){
    var arrMovie = [];
    cityData.forEach(function(movie, i){
      var allMovies = movie.data.map(m => m.movieName);
      arrMovie = arrMovie.concat(allMovies);
    });
    $('#container').html('<b>Movies: </b><br/>'+ arrMovie.join(', '))
  });
  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="UserData">
    <h1><a href="moviebooking.html">MyMovie-Ticket-Booking</a></h1>
    <select class="selectCity" id="selectCity">
        <option value="City">Select City</option>
        <option value="Bengaluru">Bengaluru</option>
        <option value="Hyderabad">Hyderabad</option>
        <option value="Guntur">Guntur</option>
        <option value="Ongole">Ongole</option>
    </select>
    <span id="welcome"> </span>
    <select class="selectTheater" id="secondselectbox">
    </select>
    <select class="selectMovie" id="thirdselectbox">
    </select>
</div>
<fieldset style="margin-top:20px;">
  <legend>Your Selection</legend>
  <div>Theater: <span id="selectedTheater"></span></div>
  <div>Movie: <span id="selectedMovie"></span></div>
</fieldset>
<div  style="margin-top:20px;">
  <input type="button" id="theaterButton" value="Get Theater"/>
  <input type="button" id="movieButton" value="Get Movie"/>
  <div id="container"></div>
</div>

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

5 Comments

I am already getting this one i want to print the movie name and theater name in html dom? when selecting the movie or theater how to do that one@Mamun
Thank you very much and i have another doubt how to put two buttons in html one for movies and another for theaters when click movies button print the all movies in json as well as theaters....@Mamun
@Babu, I have updated the answer to get theaters and movies on button click. But for that you need to change your arrays last object's data from data: 'currently not available' to data: []. Thanks again.
Thank you so much once again your Awesome @Mamun
i have i doubt when i select the movie or theater in drop down to display the only selected movie where it is playing and select the theater to display which movie is playing that theater?@Mamun

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.