Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add an onclick event to a div

I don't know if this is allowed or even possible. Is it possible to create a div element with an onclick event so that if anywhere in the div's area is clicked, the event will run.

Eg JAVASCRIPT:

var divTag = document.createElement("div"); divTag.id="new-ID";  var onClickCommand = "printWorking()"   //printworking is another method that simply print "I'm working" to the console divTag.onclick = onClickCommand; console.log("onclick command added"); console.log(divTag.onclick); parentDiv.appendChild(divTag); //This simply adds the div tag to the page. 

HTML generated:

 <!--<div id="new-ID">-->   whatever content I decide to add to the divTag.innerHTML 

I notice how theres no onclick command going through to the generated div. Id this because its not allowed?

So 2 questions then.

1:Is it possible to add onclick to a div and have it occur if any area of the div is clicked. 2:If yes then why is the onclick method not going through to my div.


How very odd:

divTag.setAttribute("onclick", "printWorking()"); 

Above works flawlessly.

All below don't and wont pass the onclick to the div.

divTag.onclick=printWorking;  divTag.onclick="printWorking";  divTag.onclick="printWorking()"; 

Multiple other variants have been tried too.

like image 696
OVERTONE Avatar asked Aug 02 '11 09:08

OVERTONE


People also ask

Can we add onclick event to div?

We can bind a JavaScript function to a div using the onclick event handler in the HTML or attaching the event handler in JavaScript. Let us refer to the following code in which we attach the event handler to a div element. The div element does not accept any click events by default.

How do you add onclick event dynamically?

Attaching the event dynamically className = 'dynamic-link'; // Class name li. innerHTML = dynamicValue; // Text inside $('#links'). appendChild(li); // Append it li. onclick = dynamicEvent; // Attach the event!

How do you call a function on a div click?

<div class="test">Click me! </div> $('div. test'). click(function(){ GetContent("xxx"); });


2 Answers

Is it possible to add onclick to a div and have it occur if any area of the div is clicked.

Yes … although it should be done with caution. Make sure there is some mechanism that allows keyboard access. Build on things that work

If yes then why is the onclick method not going through to my div.

You are assigning a string where a function is expected.

divTag.onclick = printWorking; 

There are nicer ways to assign event handlers though, although older versions of Internet Explorer are sufficiently different that you should use a library to abstract it. There are plenty of very small event libraries and every major library jQuery) has event handling functionality.

That said, now it is 2019, older versions of Internet Explorer no longer exist in practice so you can go direct to addEventListener

like image 113
Quentin Avatar answered Sep 23 '22 01:09

Quentin


Assign the onclick like this:

divTag.onclick = printWorking; 

The onclick property will not take a string when assigned. Instead, it takes a function reference (in this case, printWorking).
The onclick attribute can be a string when assigned in HTML, e.g. <div onclick="func()"></div>, but this is generally not recommended.

like image 40
Digital Plane Avatar answered Sep 20 '22 01:09

Digital Plane