I am storing some html in variable after successful ajax call and appending that html to a div. Everything is fine except the event parameter. When I am passing event as a parameter of a java script function,it's appending the img html to the div but on clicking the image I am getting 'Unexpected identifier' error. Here is the code:
var dataToAppend = "<img onclick='getImgSrc("+event+")' data-toggle='modal' data-target='#msgPhotoModal' class='modal-message-img getSrc' src='" + data[i].ImgUrl + "' />" +
When I am debugging dataToAppend variable I am getting:
<img onclick='getImgSrc([object Event])........'
and the function is:
function getImgSrc(e) {
debugger;
var src = $(this).attr('src');
$('.msgImg').attr('src', src);
}
Also var src = $(this).attr('src'); is giving me 'undefined'. What am I doing wrong?
jQueryto handle events. The callback is called with theeventobject. - ADreNaLiNe-DJdataToAppendpart please? - Lixuseventand then trying to get itssrcattribute as if it were an element. I'd tryonclick='getImgSrc(this)' ..., using the wordthisand remove concatenation. - Tyler Ropervar exampleEvt = new Event('click'); console.log(typeof exampleEvt); console.log(exampleEvt.toString());Try running that in your console and the results will look like what you're seeing. - Tom O.