개발/JavaScript

Dom 유틸함수

나태쿤 2022. 12. 28. 22:53
728x90
function fnGetIntValueById(id) {
    let intValue = document.getElementById(id).value;
    intValue = (intValue === '' ? 0 : parseInt(intValue));
    return intValue;
}

function fnGetStringValueById(id) {
    return document.getElementById(id).value;
}

function fnGetTextById(id) {
    return document.getElementById(id).textContent;
}

function fnGetCheckById(id) {
    return document.getElementById(id).checked;
}

function fnGetSelectValueById(id) {
    const selectHtml = document.getElementById(id);
    const index = selectHtml.options.selectedIndex;
    const value = selectHtml.options[index].value;
    return value;
}

 

바닐라 자바스크립트 기반으로, 프론트쪽에서 dom의 값을 가져오는일이 빈번한데,

자주사용하는 유틸 함수를 만들어봤다.

 

728x90