My JavaScript code is as follows
$.getJSON( "sample_json.json", function( data ) {
//javascript object
var someId = data['item'][0]['id'];
console.log("Outside addEventListener "+someId);
//create <tr> element
var tr = document.createElement('tr');
//bind click event
tr.addEventListener("click",function() {
console.log("inside addEventListener "+someId);
someFunction(someId);
},false);
});
//function
function someFunction(someId){
console.log("Inside someFunction "+someId);
}
Now when I click on <tr>
element it gives following in output
Outside addEventListener 150
Inside addEventListener 1
Inside someFunction 1
When I change someId
as
var someId = 150;
It works perfectly
Outside addEventListener 150
Inside addEventListener 150
Inside someFunction 150
What should I change in javascript code to get the actual id from javascript object on click event ?
Note: I have already referred the existing question on Stack Overflow. How to pass arguments to addEventListener listener function? But have not helped much.
Edit: Updated javascript code with some jquery functions, which creates data
javascript object from JSON file.