How to get div id element – Javascript
Written on
April 17, 2009 at 5:32 pm by
mkyong
Often times, we need to get our DIV tag id for form validation or decoration. The Javascript getElementById() function can help to get the DIV tag id easily.
Here is the full example of using getElementById() function to get the DIV tag id and try to use InnerHTML function to change the text dynamicly.
<html>
<head>
<title>getElementById example</title>
<script type="text/javascript">
function changeText(text)
{
elem = document.getElementById('textid');
elem.innerHTML = text;
}
</script>
</head>
<body>
<div id='textid'>I'm a Text</div>
<button onclick="changeText('Mkyong1');">Mkyong1</button>
<button onclick="changeText('Mkyong2');">Mkyong2</button>
</body>
</html>
