jhmckimm
jhmckimm

Reputation: 1066

JavaScript document.createElement() within Object

I think I'm having another dumb moment. I've looked around for the past half hour, but Google searches aren't returning anything useful. I can't find the words to describe the issue I'm having. Here's my code.

function myElement(){

    this.init = function(){
        this.display();
    }

    this.display = function(){
        var element = document.createElement("div");
    }   
}

var myElement = new myElement();
myElement.init();

The issue I'm having is that the code (.createElement("div")) isn't working. I've messed around with it, and I've tried using other things too (such as JQuery's $.create('<div></div>'); method).

I'm not entirely sure if it's a scope or referencing issue.

(Please be aware that it's 02:10AM in the morning. My brain isn't in its most active state.)

Upvotes: 0

Views: 919

Answers (2)

MikeHelland
MikeHelland

Reputation: 1159

You have to append the element to something, like another element, or the body.

 document.body.appendChild(element)

Upvotes: 0

bfavaretto
bfavaretto

Reputation: 71918

It is working, but you're not doing anything with the div. I'm guessing you want to append it to the DOM:

this.display = function(){
    var element = document.createElement("div");
    document.body.appendChild(element);
};

Upvotes: 1

Related Questions