티스토리 뷰

개발하다 input을 select로 서로 변환 시켜줘야 하는 상황이 발생했다.  

평소처럼 둘다 만들어서 show/hide하기에는 
연관되는 작업들이 많아서 다른방법이 없을까 찾아보다 
replaceWith 라는 기능이 있어서 포스팅 한다. 

 공식 api : http://api.jquery.com/replacewith/ 사용법은 공식 api에도 잘나와 있지만
 
1. $(변경될 element selector).replaceWith(변경될 element html);
2. $(변경될 element selector).replaceWith(function(){ 
 return $('',attribute)
});

ex ) 
$('#abc').replaceWith("");
$('#abc').replaceWith(function(){ 
 return $('',{type : 'text', class: 'input-text'})
});

주의점은 변환 대상 element는 삭제되고 
replaceWith한 element로 변환되니 
 속성값등 필요한 정보는 꼭 다시 적어줘야 한다.


댓글