this in this context is referred to the global one, but from my understanding of your question, you’re trying to get scoped context, so the change could be
onclick="show(event)"
To access the innerHTML from that click, you can try this way
function show(event) { //the param needs to be passed properly
var currentElement = event.target || event.srcElement;
var details = currentElement.innerHTML
console.log(details)
}
P/s: If you want to keep the global context on this, you also need to pass the param on show function.
Solution 2 :
@ Nick Vu thanks for your comment your approach is quite logical and great well what i have did is, I have assigned a unique id to every card div while calling API and then target its childnode’s innerHTMLS.
function show(e) {
console.log(e)
var details = document.getElementById(e)
console.log(details)
console.log(details.childNodes[1].src)
console.log(details.childNodes[3].innerHTML)
console.log(details.childNodes[5].innerHTML)
console.log(details.childNodes[7].innerHTML)
}
this is working around what i want as output.. thanks. Or may be at last i can do forEach loop of childnodes to make it more short.
Problem :
I am creating various cards using fetch API, this cards are holding details like (Author, Title, Cover Image, etc.).
There is a lot of cards renders there once API call my struggle is how to get specific card data/details like ( author, title etc.) of clicked card. when user clicked any of the card from these set.
function show() {
var details = this.innerHTML
console.log(details)
}
Don’t know what is the right approach or method to this…
Comments
Comment posted by Sachin Vairagi
You’re passing 1 parameter when calling “show” function but you didn’t handle it when defining the function.
Comment posted by Kuldeep Singh
@SachinVairagi thanks for the comment bro actually i tried show(this) but that not working can you help me out by providing any solution or code bro ??
Comment posted by Kuldeep Singh
@Bravo can you show the example cuase i have already mention onclick in html div even if i tried funtion show(this) thats not working too..
Comment posted by Sachin Vairagi
You can try this
function theFunction(e) { console(e.target);}
Comment posted by Kuldeep Singh
@SachinVairagi this is working somehow it’s still not the whole solution i want whole detail of that card at once not target area.. what i am looking to do is that when someone is clicked on card it will popup the window containing all details of that particular like author book title etc…
Comment posted by Kuldeep Singh
hey, thanks for the comment, actually this code pretty much near what i want but still problem is it giving clicked target element instead I want whole card details at once so when user click on any card it will popup a ,modal box containing details about this card including description …so is it possible to get all card details at once…i don’t know what i missing
Comment posted by Nick Vu
I updated my answer for the sake of your requirement, I think it works well because you already have
Comment posted by Kuldeep Singh
I appreciate your help buddy, But I think the approach is need to change here totally. actually now its getting me whole html content instead i just want single card details only which is clicked from multiple cards already there is book website so i want that single click card book details in popup modal box
Comment posted by Nick Vu
Let me try to understand your problem again, so you want to have
Comment posted by Nick Vu
@KuldeepSingh the target element on that
Comment posted by Nick Vu
That’s good tho! but I have a small comment here that if you change the HTML structure, your