How to get ID of listview item Clicked. [javascript / jquery / jquery mobile] -


i need advice on how detect <li> tap/click user, write 'id' of tap/clicked <li> localstorage, use saved localstorage retrieve data detail page.

i'm new javascript/jquery, if can provide simple example code appreciated.

i know how write localstorage, read localstorage, json data server api, generate loop listview unique id each <li>.

what need is, how use js make <li> clickable (link detail page) , write localstorage @ same time.

i have tried:

$('.liclass').click(function() { //block of code write localstorage }; 

but <li> not clickable , no key/value written localstorage. not mention detect <li> clicked (this have no idea).

please advice, thank you.

code update:

//show restaurant listing - note: not first page. link other page.  $('#restaurantlist').on("pagebeforecreate", function() {     $.getjson("http://mydomain.com/api/restaurant", function( data ) {         function rstlisting(data) {             if ($.isemptyobject(data) === true) {                 alert ('json return empty');         } else {             (i=0; i<data.length; i++){                 $('#restaurantlisting').append('<li id="' + data[i].restaurant_id + '" class="rstlist"><img src="http://mydomain.com/file/img/' + data[i].restaurant_logo + '"><h2>' + data[i].name + '</h2><p>' + data[i].city + '</p></li>');                 $('#restaurantlisting').listview('refresh');             }         }         }         rstlisting(data);     }               ); });  //listview link detail page  $(document).ready(function(){   $('.rstlist').click(function() {       var id = $(this).attr("id"); // id       alert(id);       console.log(id);   }); }); 

also tried:

//listview link detail page  $('#restaurantlist').on("pageload", function() {   $('.rstlist').click(function() {       var id = $(this).attr("id"); // id       alert(id);       console.log(id);   }); }); 

you don't need make <li> element clickable self, when add click event element, triggered when item clicked.

your problem element not loaded when event bind it. have add code inside document ready event this.

$(document).ready(function(){   $('.liclass').click(function() {       var id= $(this).attr("id"); // id   }; }); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -