Question

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.)

Était-ce utile?

La solution 2

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

 document.body.appendChild(element)

Autres conseils

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);
};
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top