onclick 함수에서 string parameter 전달하는 법

Yuri Lee·2021년 2월 18일
0

배경

ajax를 이용해서 data 처리를 하던 도중 발생한 이슈이다. 나는 데이터의 컬럼 네임을 출력하려고 했다.

<button type="button" onclick="openModal(v.columnNm)">

하지만 계속 오류가 발생했다. 😥

error

JavaScript: Uncaught SyntaxError: missing ) after argument list

해당 코드줄로 이동해도 다 제대로 ) 되어있는데, 뭐가 문제인지 싶었다. 그래서 구글에 how to use parameter in onclick event 키워드로 서칭을 했다.

이유

It looks like you're building DOM elements from strings. You just need to add some quotes around string parameter

string parameter에 따옴표를 추가하면 된다고 한다.

해결

<button type="button" onclick="openModal(\'' + v.cctvNm + '\')">

nice !! 🤗🤗🤗


Pass a string parameter in an onclick function
https://stackoverflow.com/questions/9643311/pass-a-string-parameter-in-an-onclick-function

profile
Step by step goes a long way ✨

0개의 댓글