Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Javascript function from a div

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()
        {
            alert("Test");
        }

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?

like image 475
user2002197 Avatar asked Aug 14 '26 14:08

user2002197


1 Answers

You can attach the call to a click handler:

In markup:

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

Or inside your script block:

function testFunction() {
   alert("Test");
}

var el = document.getElementById("testFunction");
el.onclick = testFunction;
like image 176
Konstantin Dinev Avatar answered Aug 17 '26 03:08

Konstantin Dinev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!