[RESOLVED]add Image to a div using Javascript

I need to insert an image to a div  in javascript,but it doesnt work . i have the follwoing code

x = $(‘<div id="’ + id + ‘">’).appendTo($(‘#text’));

x.innerHTML = "<img src=’../../App_Themes/Images/add_btn.png’ />"

or there is any way to add an image to a div in javascript providing that i have the image path like this:



Try like this:

x = $('<div id="' + id + '">');

x.html("<img src='../../App_Themes/Images/add_btn.png' />");


Hi Sanfoor,

According to your requirements, I made a test on my computer, adding image, when page loads.

For achieving your goal, please try my code as below,

<script type="text/javascript">
        onload = function f()
        { document.getElementById("insert").innerHTML = '<img src="../Image/bg.jpg"/>'; }//change the address into your image
    <form id="form1" runat="server">
    <div id="insert">   

This function is adding image into div by the id of div.

Hope this can help you.

Best regards,

Try change
hope this help
x.innerHTML = "<img src=’../App_Themes/Images/add_btn.png’ />"

Hi Sanfoor,

Thanks for your post.

As for your problem, you could refer to my below sample.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
            background-image:url(' ../../App_Themes/Images/add_btn.png');
    <script src="../../Scripts/jquery-1.8.2.js"></script>
        $(function () {
            //add <img> tag in div
            $("#imgc").html("<img src=' ../../App_Themes/Images/add_btn.png ' />");

            //or set image as backgroung image, in this way, you should set appropriate height and width to div
    <form id="form1" runat="server">
            <div id="imgc"></div>
            <br />
            <div id="imgc1"></div>

If you have any question about this issue, please post back.

Best Regards,

Fei Han

Leave a Reply