ajax - Jquery .click to show proper value from loop -
i have list of movies , when click on title of 1 want display data being extracted xml page movie title new window.
i looping through xml file list of movies in first place
$(xml).find('movie').each(function () { //adding movie title. $("body").append("<section class = 'list' id='m" + n + "'><p>" + $(this).attr("name") + "</p></section>"); say instance first movie "terminator 2 judgment day"
my last attempt using array.. still displays same title every title click first one, terminator.
$(xml).find('movie').each(function () { //adding movie title. $("body").append("<section class = 'list' id='m" + n + "'><p>" + $(this).attr("name") + "</p></section>"); //adding movie website link $("#m" + n).append("<a href='" + $(this).find("website").text() + "'>" + $(this).attr("name") + "'s website</a> <br />"); //adding other movie info in new window info.push(($(xml).find("movie").attr("name"))); $("#m" + n+" p").click(function () { mwindow = window.open("", "", "width=500,height=500"); mwindow.document.write(info[info.length-1]); }); n++ });
change
info.push(($(xml).find("movie").attr("name"))); to
info.push($(this).attr("name")); ...to insert name attribute current item in loop array, instead of name attribute first matched result $(xml).find('.movie')
edit: different approach looking for:
$(xml).find('movie').each(function (i, elm) { var $movie = $(elm), $section, movie = { id: 'm' + id, name: $movie.attr('name'), website: $movie.find('website').text() }; $section = $('<section class="list" id="' + movie.id + '"><p>' + movie.name + '</p><a href="' + movie.website + '">' + movie.name + '\'s website</a><br></section>'); $section.data('movie', movie); $('body').append($section); }); $('body').on('click', 'section.list p', function (e) { var $section = $(e.target).closest('section.list'), movie = $section.data('movie'), popuptext; popuptext = movie.name + ', ' + movie.website; mwindow = window.open("", "", "width=500,height=500"); mwindow.document.write(popuptext); }); first, loops through $(xml), creates , appends section each movie. then, of movie data gets attached section element using .data()
after loop, binds one click event listener, detects closest section clicked target, grabs it's data , further it, opening popup text specified in popuptext
hope helps.
Comments
Post a Comment