Take the 2-minute tour ×

Can I please have some help to call a function that is created in Javascript when a reference is made to a DIV in HTML.

Here is my function:

        function testFunction()

I would like the testFunction to be called when the following reference is made in HTML:

<div id="testFunction"> 

Can I please have some help to do this?

share|improve this question
What do you exactly mean by "when the following reference is made"? When the parser encounters the element with this ID? JavaScript is either executed when the parser encounters it in the HTML or as a reaction to some user interaction. What kind of interaction is "making a reference"? Your question really isn't clear. –  Felix Kling Jan 27 '13 at 21:04

1 Answer 1

up vote 1 down vote accepted

You can attach the call to a click handler:

In markup:

<div id="testFunction" onclick="testFunction()"> 

Or inside your script block:

function testFunction() {

var el = document.getElementById("testFunction");
el.onclick = testFunction;
share|improve this answer
var el = document.getEl...etc is unless becouse already have onclick attribute –  miru87 Jan 27 '13 at 20:43
@miru87 Read the "OR" :) –  Konstantin D - Infragistics Jan 27 '13 at 20:45
Sorry, my fault ;) –  miru87 Jan 27 '13 at 20:50

Your Answer


