Insert image using AJAX

I am attempting to use Ajax to dynamically add an <img> element to a div on my webpage. The image's name needs to be fetched from a database.

Although I have successfully added the <img> tag to the div and set the correct source (src) attribute for the image, I am unable to see the image itself. Instead, I only see an <img> tag with dimensions of 0 x 0 pixels. Even after adjusting the size, the image remains invisible.

When I manually access the image via the link generated in the src attribute, I am able to view the desired image. Why is this happening?

Below is the code snippet:

Trigger function for Ajax:

<div class="HotelListDescriptionContent" onclick='showDetailRoom("<?php echo $arrayroomssearch[$arrk]['room_code']?>")'>

AJAX Function:

function showDetailRoom(roomid)
{
    if(j("#detailroom"+roomid).is(":visible"))
        j("#detailroom"+roomid).hide();
    else
    {
        j.post("inc/querys/roomdetail.php",
        { 
            roomid : roomid,
        },
        function(data){
            // console.log(data);
            j("#detailroom"+roomid).html("");
            if(data == "empty")
            {
                j("#detailroom"+roomid).append($("<option></option>").append("State Not Found..."));
            }
            else
            {
                j(data.photo).each(function(i,item){
                    j("#detailroom"+roomid).html("<img scr='<?php echo SITE_URL;?>upload_photos/images/"+item+"'/>");
                })
            }
            j("#detailroom"+roomid).show();
        },
        "json");
    }
}

Target div:

<div id="detailroom<?php echo $arrayroomssearch[$arrk]['room_code'];?>" style="display:none;">

Expected output in target div (from Inspect Element):

<div id="detailroom20" style=""><img scr="http://localhost/bookingbeol/bk-admin/upload_photos/images/kamar3-1476346812.jpg"></div>

The target div is not displaying the image as intended. https://i.sstatic.net/mG9FD.png

Answer №1

Ah, I see now - you simply made a small typo. It should actually be src instead of scr. Mistakes like that are easy to make and hard to catch when quickly scanning through the code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Vue JS: dynamic reactive structure

As I delved into understanding the basics of reactivity and its syntax, I encountered an interesting problem. Take a look at this code snippet: const app = Vue.createApp({ data: dataFunction, methods: {method1: methodImpl} }) var dataObj = { tit ...

Which UL should be selected next?

I am facing an issue with a trigger that is meant to target the next UL element and apply effects to it. <div id="archives_trigger"><a class="menu_trigger">Monthly Archives</a></div> <ul id="archives_menu"> <?php wp_ ...

Reconfigure the Node application using Express to seamlessly access modules through route files

I recently created a basic app using npm express. After generating the app.js file, I attempted to add new modules but encountered an issue where I couldn't access them in the route files. For example, when trying to utilize the 'request' mo ...

div layout; sidebar movements occur after appending div to the header

Having trouble mastering layouts, I am attempting to create a full-width header that includes a left sidebar and a right sidebar. The body is split into 5 sections: full width, left and right sidebars, center content with 3 columns, and the footer mirrorin ...

Switching the file format from HTML to .ASP resulted in the loss of the website's design elements and images

After initially creating an HTML file, I decided to rename it with a .asp extension for uploading online. To do this, I copied the original HTML file and moved it into a folder containing .asp files. However, after replacing the existing file, my website l ...

Activate the action using the onclick interaction

window.addEventListener(mousewheelEvent, _.throttle(parallaxScroll, 60), false); My current setup involves an event listener that responds to a mousewheelEvent by executing a function. However, when attempting to directly trigger this function on a separa ...

The ion-slide-box does not function properly the first time the HTML page is loaded

Upon loading the page, my ion-slide-box does not function correctly. However, if I refresh the page, it works as expected. I have tried referring to some links but have been unable to resolve this issue on my own. Any assistance would be greatly appreciate ...

Is there a way to utilize AJAX for redirecting the page?

Utilizing the Infragistics control in my application, I encountered an issue. When a user is deleted from my system, I wish to redirect them to a "not authorized" page. However, attempts using HttpContext method and window.location through JavaScript have ...

Ruby application requires refreshing for Ajax deletions to take effect

I am currently working on developing a task management app using Rails. Each to-do list in the app contains multiple tasks, and my issue lies in deleting a completed task with Ajax without having to manually refresh the page for it to vanish. As I am still ...

Using Kendo's Angular Grid to link data sources

I'm currently facing a minor issue with the Kendo Grid for Angular. My attempt to bind data after fetching is resulting in the following error: ERROR TypeError: Cannot read properties of undefined (reading 'api') This indicates that this. ...

What is the best way to incorporate and execute a separate JavaScript file in a Node application using Express JS?

I am attempting to configure a javascript file that is responsible for grabbing an RSS feed and storing the information in a database. Originally, I had this file called by the HTML page, but now I want it to continuously run in the back-end (constantly re ...

Is it possible to dynamically override inline styles?

My HTML code is as follows: <div title="remove css"style="position:relative;">Remove my style</div> After the page loads, I need to completely remove the position style attribute. Due to limitations, I cannot override the CSS. Is there a way ...

Utilizing Javascript to export modules and call multiple functions simultaneously

Is there a way to automate the calling of all functions within a module instead of individually selecting each one? For instance: bettermovies.js module.exports={ printAvatar: function(){ console.log("Avatar"); }, printLord: funct ...

Enable only a single radio button to be selected in HTML

I am facing an issue with my radio buttons where I only want one to be selected at a time. Despite giving them the same name, all four can still be selected simultaneously. <h1>Portion</h1> <input type="radio" name="portion_n ...

Ways to update the UI dynamically in Angular without the need to refresh the entire page

On my page, I have multiple charts and I'm facing an issue where the chart UI does not update immediately when I try to make a delete call by clicking the delete button. I always have to refresh the browser to see the changes. I have provided the ful ...

Service in AngularJS designed specifically for storing and managing SEO metadata

I stumbled upon a tutorial that introduced me to using a service for dynamic SEO metadata here: However, I encountered an issue - It seems like the service is not accessible outside of the controller's view. <div ui-view></div> Here is t ...

Guide to activating the isActive status on a live link within a map iteration utilizing the NEXTUI navigation bar

Check out the new NEXTUI navbar I'm using: I am having trouble setting the isActive property on the active link in my NavBar component in Next.js. I couldn't find much help on Google, so I'm hoping someone here has experience with this or k ...

Having trouble obtaining data from the database and showing it on the webpage

I am encountering an issue with displaying content from a database named resources and a table within it also named resources. The code snippet causing trouble is as follows: mysql_connect('localhost', 'username', 'password', ...

Creating a dynamic variable within a for loop and calculating the total of values to assign to that variable

Is it possible to dynamically create variables and add values to them repeatedly? For example: var totalIncomeCr = 0, totalIncomeDr = 0; for (var k = 1; k <= numOfYears; k++) { if(response[i]["AmountType" + k] == "Cr") { if(response[ ...

What is the best way to assign a default value in mat-select when using an ngFor loop?

Is there a different approach to setting a default value in mat-select within an *ngFor loop? I'm having trouble accessing the element in the array from the loop as desired. .ts file: persons: Person[] = .. //consist of Person with name and age .htm ...