jquery - access specific data from fetched json file -
jquery - access specific data from fetched json file -
hi i'm new frontend development, have fetched json info , added html list <li>
items problem when click on list format json want specific info that particular link clicked logged console window.
class="snippet-code-js lang-js prettyprint-override">var data1={"users":[ { "deptime":"21:30", "arrival":"23:30", "duration":"1h 45m", "price":"2,400", "planename":"auditore", "stops":"non-stop", "image":"indiago" }, { "deptime":"21:30", "arrival":"23:30", "duration":"1h 45m", "price":"2,400", "planename":"auditore", "stops":"non-stop", "image":"indiago" } ] }; var output="<ul>"; for(var in data1.users) { output +="<li>"+ "<a href='#'>"+"<table>"+ "<tr>"+ "<td>"+"<input type='radio' name='radio'>"+"</td>"+ "<td>"+"<img src=''>"+"</td>"+ "<td>"+data1.users[i].deptime+"</td>"+ "<td>"+data1.users[i].arrival+"</td>"+ "<td>"+data1.users[i].duration+"</td>"+ "<td>"+"</td>"+ "<td>"+data1.users[i].price+"</td>"+ "</tr>"+ "<tr>"+ "<td>"+"</td>"+ "<td>"+data1.users[i].planename+"</td>"+ "<td>"+"</td>"+ "<td>"+"</td>"+ "<td>"+data1.users[i].stops+"</td>"+ "</tr>"+ "</table>"+"</a>" + "</li>"; } output += "</ul>"; document.getelementbyid("placeholder").innerhtml=output; var add="<h1>"; $("#placeholder a").click(function(){ event.preventdefault(); var value= //this run problem //i want cost , deptime value of particular <li> element clicked on console.log(value); }); add+="</h1>"; document.getelementbyid("add").innerhtml=add;
class="snippet-code-html lang-html prettyprint-override"><!doctype html> <html> <head> <title></title> </head> <body> <div id="placeholder"></div> <div id="add"></div> </body> <script type="text/javascript" src="jquery-1.11.1.min.js"></script> <script type="text/javascript" src="script.js"></script> </html>
you can take needed value index of li
element:
$("#placeholder a").click(function(event){ event.preventdefault(); var index = $(event.currenttarget).parent().index(), value = data1.users[index]; console.log(value); });
http://jsfiddle.net/0f10qt27/2/
jquery json
Comments
Post a Comment