const dataContainer = document.querySelector('.elements')
data.map((book) => dataContainer.innerHTML +=
`<div class='book' onclick="alertClick(${book})">${book.name}</div>`
)
const alertClick = (book) => {
console.log(book)
}
Asked
Active
Viewed 367 times
-1

Saif Mohamed
- 9
- 1
-
1You need quotes in the parentheses in the onclick call to `alertClick()` – Pointy Aug 13 '22 at 12:59
-
Does this answer your question? [SyntaxError: Unexpected Identifier in Chrome's Javascript console](https://stackoverflow.com/questions/5000114/syntaxerror-unexpected-identifier-in-chromes-javascript-console) – Michael Freidgeim Mar 20 '23 at 12:22
1 Answers
0
As mentionned by @Pointy
in comments you need quotes in the parentheses in the onclick
event handler!
const dataContainer = document.querySelector('.elements')
data = ['Clean Code', 'The C programming language']
data.map((book) => dataContainer.innerHTML +=
`<div class='book' onclick="alertClick('${book}')">${book}</div>`
)
const alertClick = (book) => {
console.log(book)
}
<div class='elements'/>

XMehdi01
- 5,538
- 2
- 10
- 34
-
1It's generally best not to answer "simple typographical error" questions. Instead, flag or vote to close them as such. Questions like these are not likely to be of any help to anyone in the future and therefor just take up unnecessary space on SO. – icecub Aug 13 '22 at 13:03