Using JavaScript to Write into a DIV or SPAN

4 Jan 2011
1 comment

Here’s a very simple way to insert words or letters or numbers into a DIV or SPAN using JavaScript.

For me, I usually only use this for debugging my JavaScript codes. Here’s one example on how to count the number of clicks on a button and display the increasing/decreasing click hits in a DIV simultaneously.

Click Counter using JavaScript

First, create an HTML file and throw these codes into the <body> tag:
<div id="clicker"></div>
<input name="button" type="button" onclick="clickCounter()" value="Click Me!" />

The clicker DIV will be the placeholder that will display the values sent by the JavaScript function, when the button is clicked.

Then in your <head> section put these JavaScript codes in:
var clickcount = 0;
function clickCounter() {
   clickcount += 1;
   document.getElementById("clicker").innerHTML = clickcount;

Now that’s it! Go click that button to see the numbers increasing.
Wow that’s so interesting. -___-

Tags -
Author - Azhar Kamar
1 Lonely Comment...
  1. nishant

    thanks it works for me.

Write a Comment! NOW! :)