I'm trying run the onclick function in backticks, when i clicked.
backticks :
socket.onmessage = function(event){
console.log(event.data);
const data = JSON.parse(event.data);
data.map((person)=>{
console.log(person)
$("#unknown-persons").append(`
<div class="media p-l-5">
<div class="pull-left">
<img width="40" src="${person.image}" alt="">
</div>
<div class="media-body">
<small class="text-muted">${new Date(parseInt(person.time))}</small><br/>
<a data-toggle="modal" href="#modal-unknown-person" onclick="labelingToUser(person)">${person.name} ${person.lastname}</a>
</div>
</div>
`)
})
}
onclick function:
function labelingToUser(){}
But i can not pass the person object as parameter to my onclik function. How can i do ?
Person is not global object, so its not accessible to append function.
Try this
var personglobal;
socket.onmessage = function(event){
console.log(event.data);
const data = JSON.parse(event.data);
data.map((person)=>{
personglobal = person;
console.log(person)
$("#unknown-persons").append(`
<div class="media p-l-5">
<div class="pull-left">
<img width="40" src="${person.image}" alt="">
</div>
<div class="media-body">
<small class="text-muted">${new Date(parseInt(person.time))}</small><br/>
<a data-toggle="modal" href="#modal-unknown-person" onclick="labelingToUser(personglobal)">${person.name} ${person.lastname}</a>
</div>
</div>
`)
})
}
you can use JSON.stringify(person
) method to convert the object ot json string then pass it to the function calling as following:
socket.onmessage = function(event){
console.log(event.data);
const data = JSON.parse(event.data);
data.map((person)=>{
console.log(person)
$("#unknown-persons").append(`
<div class="media p-l-5">
<div class="pull-left">
<img width="40" src="${person.image}" alt="">
</div>
<div class="media-body">
<small class="text-muted">${new Date(parseInt(person.time))}</small><br/>
<a data-toggle="modal" href="#modal-unknown-person" onclick="labelingToUser(${JSON.stringify(person)})">${person.name} ${person.lastname}</a>
</div>
</div>
`)
})
}
function labelingToUser(person){}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.