-2

I want to create image object when user selecting a file from html page.

How can i create image object for dynamic images. Can anybody share me the sample snippet .

Thanks in advance

user1232780
  • 119
  • 2
  • 4
  • 6

2 Answers2

2

Basically:

  • You can create an img element like any other element: createElement. (There's also new Image() which is pretty much the same thing, just a legacy mechanism.)

  • You can set its source by setting the src property, which can be a file:// URL the user supplies...

  • ...or you can let them choose via an input type="file" element if their browser supports using the File API; see my answer to this other question for getting access to image data that way.

  • You can put the img element in the document by appending it to whatever other element you want to append it to, via appendChild or related methods.

  • You can find an existing element using getElementById, getElementsByTagName, etc., or (on most browsers but not all) querySelector / querySelectorAll.

In addition the various specifications linked inline above, you may also find the new work-in-progress HTML5 specification useful.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

The following code will create an empty image object and it will set its src.

var img = new Image();
img.src = "";

We are unable to identify your question & the purpose. please make a jsfiddle with minimum test case. Or explain your question clearly.

Praveen Vijayan
  • 6,521
  • 2
  • 29
  • 28