In JavaScript, you can use getElementById() fucntion to get any prefer HTML element by providing their tag id. Here is a HTML example to show the use of getElementById() function to get the DIV tag id, and use InnerHTML() function to change the text dynamically.

HTML…

<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>
  1. If button “Mkyong1″ is clicked, the content of the div element, with an Id of “textid”, will be changed to “Mkyong1″.
  2. If button “Mkyong2″ is clicked, the content of the div element, with an Id of “textid”, will be changed to “Mkyong2″.
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website mkyong.com is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts