2

I want to display the image in from a <button> but there seems to be some kind of problem.

I can't display the image. I've read some articles concerning about Javascript and OOP, I just don't know if I used them correctly as it is said.

Now going to my code, here what it looks like:

function myFunction() {
    function Image(src, height, width) {
        this.src = src;
        this.height = height;
        this.width = width;
    }
    image.prototype.display = function () {
        document.body.appendChild(image);
    }
    var image = new Image("image.jpg", "200", "200");
    image.display();
}
}

Hope you help me out :)

6
  • Im not an expert. But should the top function be a class instead? Commented Mar 8, 2014 at 0:15
  • That function you mentioned is a function to be called from a button. Commented Mar 8, 2014 at 0:16
  • Try placing the inner function outside - like a sibling of the other one, i think the problem lies there Commented Mar 8, 2014 at 0:18
  • Image already exists as a constructor. Commented Mar 8, 2014 at 0:21
  • 1
    I tried it but nothing happens. Perhaps you could show me some code concerning this, so I could get a idea on how to do it correctly? Commented Mar 8, 2014 at 0:24

2 Answers 2

1

You need to create a DOM element and add the attributes to it. Here's a working example on jsfiddle:

http://jsfiddle.net/LADXL/

function myFunction() {

    function Image (src, height, width) {
        this.src = src;
        this.height = height;
        this.width = width;
    }

    Image.prototype.display = function() {
        var img = document.createElement('img');
            img.setAttribute("src", this.src);
            img.setAttribute("height", this.height);
            img.setAttribute("width", this.width);
        document.body.appendChild(img);
    }

    var image = new Image ( "http://www.zwani.com/graphics/hello/images/10.gif", "200" , "200" );

    image.display();
}

myFunction();
Sign up to request clarification or add additional context in comments.

2 Comments

Where you're wrong: you can't redefine Image's constructor. Where you're right: Image.prototype.display (sort of; prototyping existing API isn't considered kosher).
This works but I need a FileReader() for the <input type=file> and a submit button for it to work. Thank you for this :)
0

Seens as you're defining your own Image class, you don't appear to be providing anything DOM-worthy to be rendered.

Your display method might look something a bit more like (note the capitalisation of Image):

Image.prototype.display = function() {
    // probably actually want to put this in the constructor
    // i.e., one img element per Image object
    var image = document.createElement("img");
    image.src = this.src;
    image.style.height = this.height;
    image.style.width = this.width;
    // also probably don't want to append to body every time
    // display is called, style.display = "inline" or similar
    // would be better with a corresponding hide method
    document.body.appendChild(image);
}

3 Comments

I did something like this function myFunction(src,height,width) { this.src = "image.jpg"; this.height = 200; this.width = 200; Image.prototype.display = function() { var image = document.createElement("img"); image.src = this.src; image.style.height = this.height; image.style.width = this.width; document.body.appendChild(image); } Image.display(); } but I don't know if this was correct or not.
What is the point of document.createElement("img"); when you can just do new Image()?
@Derek朕會功夫 none - if you're using the core Image class. The OP is using their own home-brewed Image class. For what purposes, I don't know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.