Hi All:

I am trying to load an image into a div using AJAX.
Instead on loading the image, I get �PNG as the result in the div.

Does anyone know how to load an image into a div?

Best Regards,
dennishall

Recommended Answers

All 5 Replies

What you are actually (incorrectly) doing now is inserting the "BINARY" image content into the div. What you need to do is to insert and <IMG> tag into the div and set the SRC attribute to the path of the image. The browser will take care of the rendering.

commented: nice, getting all that from such a short post +1

What you are actually (incorrectly) doing now is inserting the "BINARY" image content into the div. What you need to do is to insert and <IMG> tag into the div and set the SRC attribute to the path of the image. The browser will take care of the rendering.

Thanks for the reply hielo:

My bad, I failed to mention that I'm loading the image via javascript.
The onclick syntax is the javascript('file', 'div_id')

I'm at a loss as to where I can insert the image tag in the following line of code:
<a href="javascript:void(0)" onclick="ajaxpage('image.png', 'rightcolumn');">Image 1</a>

Best Regards,
dennishall

function ajaxpage(url,container)
{
 if(/\.(gif|png|jpg)$/i.test(url))
 {
   document.getElementById(container).innerHTML='<img src="'+url+'" alt="" />';
  return;
 }
 //whatever you currently have, should remain untouched
 ...
}
function ajaxpage(url,container)
{
 if(/\.(gif|png|jpg)$/i.test(url))
 {
   document.getElementById(container).innerHTML='<img src="'+url+'" alt="" />';
  return;
 }
 //whatever you currently have, should remain untouched
 ...
}

Thanks hielo:

I inserted the javascript you supplied into line 3 of the ajaxpage script, verified if I needed to change anything else to avoid conflict. I did not need to change anything else.

It worked out of the box.
This issue is resolved.

UR the best,
dennishall

Glad to help Little Grasshopper.

Regards,
Hielo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.