본문 바로가기

HTML 버튼에 JavaScript 함수 호출하는 기능 넣기

2020. 7. 7.

JavaScript 함수를 만들어 준 뒤 

linkSet = {

  makeBtnReserveLink : function(this) {
              location.href = 'reserve.html' + '?id=' + getDisplayInfoId.id;
			}
            
}

 

동작하고 싶은 버튼의 onclick 안에다가 호출 명령어를 넣어주면 버튼이 클릭했을 때 위 함수가 실행 된다.

<button type="button" class="bk_btn" onclick ="linkSet.makeBtnReserveLink()">

 

 

 

보통 target 객체를 많이 이용하는데, 클릭한 엘리먼트 정보를 넘기고 싶을 땐 this를 이용하여 넘겨주면 된다.

<button type="button" class="bk_btn" onclick ="linkSet.makeBtnReserveLink(this)">


linkSet = {

  makeBtnReserveLink : function(this) {
              location.href = 'reserve.html' + '?id=' + getDisplayInfoId.id;
			}
            
}

 

댓글