Adding a click handler to Div

Hi there,
I have the following test code.  Basically, I want to make the testDataDiv invisble/visible when clicking on the TestTitleID.
I am not getting this to work.  For some reason the click callback is not called.  Can you please help with this.  Thank you.

 var jsonArray = [{Number:200, Date:24, Time:"2:2:2"}, {Number:200, Date:22, Time:"12:2:2"}, {Number:200, Date:24, Time:"21:2:2"}];

   var createGUI =  function(jsonArray) {
       var wrapperDiv = document.createElement("div");
       var titleDiv = document.createElement("div"); = "TestTitleID"; = "2px solid black";

       var titleNum = document.createElement("label");
       titleNum.innerHTML ="Number" + ":" + "200";

       var testDataDiv = document.createElement("div"); = "TestDataDivID";
       var testDataTable = document.createElement("table"); = "100%";

       var testDataHeader = document.createElement("thead");
       testDataHeader.setAttribute("style", "border:1px solid grey; background-color:#d4d3d3;");
       var testDataHeaderDate = document.createElement("th");
       testDataHeaderDate.innerHTML = "Date";
       var testDataHeaderTime = document.createElement("th");
       testDataHeaderTime.innerHTML = "Time";

       var titleRow = document.createElement("tr");

       titleRow.setAttribute("style", "border:1px dotted black; background-color:#d3d3d3;");

       var testDataBody = document.createElement("tbody");

           var TableRow = document.createElement("tr");
           var colDate = document.createElement("td");
  = "center";
           colDate.innerHTML = entry.Date.toString();
           var colTime = document.createElement("td");
  = "center";
           colTime.innerHTML = entry.Time.toString();

       return wrapperDiv;

    var addToggleEvent = function(){
        return function(){
            document.getElementById("TestDataDivID").slideToggle(500, function(){
                    console.log("Changing to invisible");
                } else{
                    console.log("Changing to visible");
    var __addCollapseHandler = function(){

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
Just add this to the end of your code:
$(function() {
    $("body").on("click", "#TestTitleID", function() {

Open in new window

Essentially, all your doing is making sure the event handler binds to an element that exists when the page loads.  Your elements are not there yet.

The other way is to initialise the event handler after creating and inserting the elements but I find the above easier :)
Tom BeckCommented:
I see two problems with your existing code.
1.) You are mixing jQuery and javascript on this line:


What you want is this:

document.getElementById("TestTitleID").onclick = addToggleEvent;

2.) You have a function assigned to a variable returning an anonymous function. Not working. Just do this:

var addToggleEvent = function(){
            $("#TestDataDivID").slideToggle(500, function(){
                    console.log("Changing to invisible");
                } else{
                    console.log("Changing to visible");

Updated jsfiddle.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.