본문 바로가기

개발하자/자바스크립트

text value값을 id로 이용하기 eval,getElementById

<html>
<head>
<script>
window.onload=function(){
b.onclick=function(){
t.value.style.background="red";
}
}
</script>
</head>
<body>
<input type="text" id="t">
<input type="button" id="b" value="바꾸기">
<input type="button" id="b1">
<input type="button" id="b2">
</body>
</html>

 

 

 

<html>
<head>
<script>
window.onload=function(){
b.onclick=function(){
var obj=document.getElementById(t.value);
obj.style.background="red";
}
}
</script>
</head>
<body>
<input type="text" id="t">
<input type="button" id="b" value="바꾸기">
<input type="button" id="b1">
<input type="button" id="b2">
</body>
</html>