0

I have to add click event to the dynamic list view. When I click this list it redirect to more detail page. I am fetching the list of Hotels available in particular area and insert into list-view. Now when I click any particular list Hotels redirect to more detail page.
check the following image list view of list of hotels available. Every hotel have unique id So when I click any list it will use that unique hotel id and fetch more details information of that hotel from server and show on one dedicated page for that particular Hotel. My Question is How I add click even on dynamic list view and pass that unique Hotel Id so that later I am able to fetch more information from server using that Hotel Id.

enter image description here

My script code, How to add click even in dynamic list

<script> 
            $(document).ready(function(){ 
                $("#btnReg").click(function(){ 
                    $("#listHotelsOptions").empty();
                    var distance = document.getElementById('distance_id').value; 
                    var output=""; 
                    var hiddenId="";
                    $.ajax({ 
                            url:"http://192.168.1.4/Experiements/webservices/getHotels.php", 
                            type:"GET", 
                            dataType:"json", 
                            data:{type:"login", Distance:distance}, 
                            ContentType:"application/json", 
                            success: function(response){ 
                            console.log(response) 
                                $.each(response, function(index,value){                                                                 
                                          hiddenId+='<li  type="hidden">'+value.Hotel.Id+'</li>';
                                          output+='<li ><a href="#" style="text-decoration:none;"> <img alt="chef" src="./images/chef.min.png" width="20px" height="20px" >'+value.Hotel.Name+' has'+value.Hotel.Romms+'</a></li>';
                                }); 
        $("#listHotelsOption").append(output).listview().listview('refresh'); 
                        }, 
                            error: function(err){ 
                            alert(JSON.stringify(err)); 
                        } 
                    }) //ajax 
                }); //click 
            }); //ready 
</script>
Neelabh Singh
  • 2,600
  • 12
  • 51
  • 88

3 Answers3

1

Try this

$("#btnReg").on('click',function(){ 

}); //click 

EDIT:

$.each(response, function(index,value){                                                                 
  hiddenId+='<li  type="hidden">'+value.Hotel.Id+'</li>';
  output+='<li class="hotel"  ><a href="#" style="text-decoration:none;"> <img alt="chef" src="./images/chef.min.png" width="20px" height="20px" >'+value.Hotel.Name+' has'+value.Hotel.Romms+'</a></li>';
}); 

$(".hotel").on('click',function(){ 

}); //click 

EDIT2

$(".hotel").live('click',function(){ 

}); //click 

$(".hotel").delegate('click',function(){ 

}); //click 

EDIT3

'<li class="hotel" id="'+value.Hotel.Id+'"  ><a href="#" style="text-decoration:none;"> <img alt="chef" src="./images/chef.min.png" width="20px" height="20px" >'+value.Hotel.Name+' has'+value.Hotel.Romms+'</a></li>';

$(".hotel").live('click',function(){ 
  var id = $(this).attr('id');
  alert(id);
}); //click 
Kiren S
  • 3,037
  • 7
  • 41
  • 69
  • $(".hotel").on('click',function(){ alert("1"); }); not working.. alert is nor showing.. – Neelabh Singh Jan 08 '15 at 06:12
  • now It is working, thanks, But my need is that when I click any list it get unique Id and using that Id I have to fetch the data and put that information into separate page Which has like name of Hotel ,price , image of rooms, and all other information, That page design will be same for every hotel and It will have id for every element like for price, image of room so I have to just assign new fetch value.. – Neelabh Singh Jan 08 '15 at 06:58
  • but is was not working. and var id = $(this).attr('id'); var id = $(this).attr('Id'); I try both..., please check edit 3 and make id to Id.. – Neelabh Singh Jan 08 '15 at 07:11
  • I am tring the following..$(".hotel").live('click',function(){ var id=$(this).attr('value.Chef.Id'); alert(id); }); – Neelabh Singh Jan 08 '15 at 07:16