lundi 2 octobre 2017

get item key on item click

i have a table with id= bigTable in which i append some items retrieved from my firebase database. Now what i want is clicking on an item to get its key. My function is below:

var itemKey;  

//READ FROM DATABASE
function readFromDatabase() {


    var database = firebase.database();
    database.ref("posts").child(currentCountry)     
        .on('value', function(snapshot){

    if(snapshot.exists()){
        var content = null;


        $('#bigTable').empty();          


        snapshot.forEach(function(data){                

            var val = data.val();

            itemKey = data.key;


            content +='<tbody>';
            content +='<tr>';
            content +='<td width="100px" height="100px">';
            content +='<img src="' + val.imageString + '" width="100%" onClick="getKey()">'+'</img>';

            content +='</td>';

            content +='<td>';
            content +='<table>';
            content +='<tbody>';    

            content +='<tr>';   
            content +='<td>' + val.country+ '</td>';   
             content += '</tr>';

            content +='<tr>';   
            content +='<td>' + val.category + '</td>';   
             content += '</tr>';

            content +='<tr>';   
            content += '<td>' + val.city + '</td>';                
            content += '</tr>';


            content +='</tbody>';
            content +='</table>';
            content += '</tr>';
            content += '</tbody>';          


        });

        $('#bigTable').append(content);
    }
});


}   



 function getKey(){

console.log(itemKey);

}

But when i click on an image of an item, what i get is always the key of the first item. So how can i get the key of the item i click?




Aucun commentaire:

Enregistrer un commentaire