검색결과로 받은 places_name을 버튼클릭시, function으로 넘기려 합니다

https://apis.map.kakao.com/web/sample/keywordList/
위 예제를 사용하였습니다.

예제에서
function displayInfowindow(marker, title) {
var content = ‘<div style=“padding:5px;z-index:1;”>’ + title + ‘</div>’;

infowindow.setContent(content);
infowindow.open(map, marker);

}

부분을
function displayInfowindow(marker, title, places) {
var content = ‘<div style=“padding:5px;z-index:1;”>’ + title + ‘</div>’;

content += '<form>' +
           '<input type="button" value="저장" onClick="messege_print(' + places.plcaes_name + ');">' +
           '</form>';

infowindow.setContent(content);
infowindow.open(map, marker);

}

function message_print(places_name){
alert(“저장되었습니다.”);
}
로 수정을 하여 사용하려 합니다.

근데 이상하게 alret창이 뜨지 않아, onclick안의 places.places_name을 places.x로 하니
잘 작동 하더라구요.
왜인지, 해결법은 무엇인지 여쭙고자 작성합니다ㅠㅠ

이스케이프 문자를 사용해서 함수 내의 문자열을 작은 따옴표로 감싸주어야 합니다.
places.x 경우 숫자이므로 문제가 없지만 places_name은 문자열이라서 에러가 발생합니다.

아래 코드 참고해주세요.
'<input type="button" value="저장" onclick="print(\''+title+'\' )">'

1개의 좋아요

감사합니다.^^
잘 작동 되네요.

혹시 places 오브젝트도 뭘 써줘야 하는지 여쭤봐도 될까요?
’<input type=“button” value=“저장” onclick=“print(’ + places + ')”>'
이렇게는 안넘어 가는것 같아서요…

아래처럼 HTMLElement로 content를 구성하고
클릭 이벤트를 등록하여 object 데이터를 넘기는 방법이 있습니다.
구현에 따라 방법의 차이는 조금씩 있으니 참고만 해주세요.

var form = document.createElement('form');
var input = document.createElement('input');
input.type = 'button';
input.value = '저장';

input.onclick = function() {
    message_print(places);
};

form.appendChild(input);