0

I have this function:

//The img_src is a source of an image 
function myid_templates_editor_create_image(img_src, w, h){ 
    console.log('image source : ' + img_src);
    var body = document.querySelector('body');          
    var image = document.createElement('image');    
    image.id = 'myid_templates_editor_image';   
    image.src = img_src;                
    body.appendChild(image);    
}

After the function is invoked, it successfully creates an image element and append it to the body but the image doesnt show. Why? img_src has the following value:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAYAAAA+s9J6AAAACXBIW…AkBAAAJAQAkBAAAJAQAEBCAABAQgAACQEAAAkBoAr47wDsSs6PZMN9tgAAAABJRU5ErkJggg== 
5
  • IW…Ak - looks like a truncated string Commented Nov 18, 2015 at 5:26
  • 1
    The image tag is <img>, is it not? Commented Nov 18, 2015 at 5:28
  • 1
    naa.. that value is a base64 encoded value of image.. Which means the argument img_src passed in is not the url to an image but the image itself! Commented Nov 18, 2015 at 5:29
  • @VijayDev --> So how can I make the image load? Commented Nov 18, 2015 at 5:38
  • @Aliyah, I tried to decode that img_src value over here, but it turns up as inavalid/empty image. CAn you make sure that the argument img_src value is a valid base64 encoded image value. I also think that value is a bit small. Normally encoded values should have huge # of chanracters Commented Nov 18, 2015 at 5:43

1 Answer 1

3

use img instead of image in createElement

function myid_templates_editor_create_image(img_src, w, h){ 
    console.log(img_src);
    var body = document.querySelector('body');          
    var image = document.createElement('img');    
    image.id = 'myid_templates_editor_image';   
    image.src = img_src;                
    body.appendChild(image);    
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, nice catch! Also its not ddocument.createElement, its document.createElement
ddocument.createElement is typo

Your Answer

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