Button HTML을 삭제하고 TEXT 형식으로 교체하는 방법

2019-01-25 (금) 14:02:36 8,220
Copy
// 실행전 

<td class="td_numbig text_right">
  <button id="search" onclick="completion(this);">실행 중</button>
</td>

 

 

// 실행 후

<td class="td_numbig text_right">
  완료
</td>

completion 함수를 호출 하게 되면 기존 button html을 삭제하고 '완료'라는 글자를 생성하고 싶습니다.

Copy
//제가 고안한 방법

 

function completion(el) {

 el.replaceChild(document.createTextNode("완료"), el.childNodes[0]);

}

이렇게 하면 text는 변경되지만 버튼은 삭제가 안되더군요 ㅠㅠ

혹시 방법이 있을까요?

jquery 쓰지 않고 순수 javascript로 구성하고 싶습니다.!!!!

|

답변 2개

부모로 한번 타고 올라가면 되겠네요

Copy
function completion(el) {
    /*
    el.parentElement.appendChild(document.createTextNode("완료"));
    el.remove();
    */
    el.parentElement.replaceChild(document.createTextNode("완료"), el);
}
2019-01-25 (금) 14:09:13

<div id="test"><button id="search" onclick="completion(this);">실행 중</button><div>

버튼을 div로 감싸고 

스크립트에서 $("#test").html('실행');

이렇게 처리해도 될거같네요 html 자체를 바꿔야한다면..

답변을 작성하려면 로그인이 필요합니다.