"Can you suggest an image that can be used as a placeholder

.mydiv(style="background-image: url('"+pic+"');" onerror="this.style.background-image:url('http://www.naijaticketshop.com/images/default_profile.jpg'); ")

Although it is common to use onerror for placeholders, I'm experiencing issues with it working for background-image. Any advice on how to resolve this?

Answer №1

<img src="my.png" alt="profile picture" onerror="this.src = 'http://www.naijaticketshop.com/images/default_profile.jpg';" />

or using jQuery

// Replace source
$('.mydiv').error(function(){
        $(this).css('background-image', 'url(http://www.naijaticketshop.com/images/default_profile.jpg)');
});

Answer №2

Remember to use this.style.backgroundImage instead of this.style.background-image

Answer №3

You cannot use onerror with a <div> element. The onerror attribute can only be used with

<img>, <object>, <script>, <style>
elements. Therefore, you must replace the <div> with an <img> tag if you want to utilize the onerror functionality. Alternatively, you can refer to this helpful resource on Stack Overflow for guidance: How to replace nonexistent images with a placeholder in asynchronous loading from database

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

Angular JS Directive is a powerful feature that allows developers

There are three HTML screens available. Screen1.html: <button id="screenbtn"></button> Screen2.html: <div id="sctmpl"> <label>Label for screen two</label> </div> Screen3.html: <div id="sctmpl1"> <l ...

Tips for extracting the content from a <span> tag within an iframe

In my setup, I have two iframes labeled Test1 and Test2. Each iframe contains the following code snippet with the goal of retrieving the value '24' from it. <div class="Test-font"> <h6 class="Test_something d-inline">Available MQ ...

Ways to achieve a blurred background effect on the body using CSS

I've been experimenting with setting a background color using linear gradient, but now I want to add a blur effect to it. I've tried various codes but haven't had any success so far. Here is the CSS code I have been working on: body{ ...

The HTML5 Canvas is failing to render lines beyond the boundaries of 2040 in both directions

Update: After running the code on a more powerful computer, the grid rendered correctly. Could it be a hardware limitation? The issue occurred on a Samsung series 3 Chromebook. My suspicion is that it might have to do with attempting to draw too many lines ...

Creating a custom backdrop for your kaboom.js webpage

I created a kaboom.js application and I'm having trouble setting a background for it. I've searched online extensively and attempted different methods on my own, but nothing seems to be working. (StackOverflow flagged my post as mostly code so I ...

Introducing space between div elements changes the arrangement of columns

I am facing an issue with my layout consisting of 6 div tags divided into 2 rows of fixed height. Whenever I try to add a margin around the divs, the rightmost div gets pushed down to the next row. How can I solve this problem? HTML: <div class="contai ...

Drop down selection causing Highchart display issue

I'm experimenting with displaying a div that contains a Highchart in this code snippet: http://jsfiddle.net/ot24zrkt/129/ This line should reveal the container: $('#container' + $(this).val()).show();? Fiddle code: <script src="https:/ ...

What is the best method to locate and reference a specific string within an HTML document using Python?

My specific need involves two separate html pages. One page consists of hyperlinks while the other contains detailed responses corresponding to those hyperlinks. What I am looking for is, when I click on a hyperlink in the first html page, it should dire ...

javascript retrieve images from an array

<ul class=""> <li><img src="" alt=""/></li> <li><img src="" alt=""/></li> <li><img src="" alt=""/></li> <li><img src="" alt=""/></li> <li><img src="" ...

Unexpected results can occur when using ngClass and CSS with all unset

Within my Angular 4 project, I am utilizing ngClass on an object that contains a CSS class applied with unset: all within it. Despite knowing that ngClass adds its properties, the expected result was for all values to be unset and the style elements from n ...

JavaScript allows you to export a variable from a function

I simply want to access the value of the userId variable for any use. async function getMyData(){ const token = ''; spotifyApi.setAccessToken(token); const data = await spotifyApi.getMe(); let userId = data.body.id; retu ...

What is the reason the child component is not being displayed?

The code within the APP.js component looks like this: import React from "react"; import Exam from "./exam.js"; export default function App() { return ( <Exam> <h1>hashemi</h1> </Exam> ); } Similarly, the ...

What methods are available to generate dynamic shapes using HTML?

Looking to create an interactive triangle where users can move vertices or sides, updating angles in real-time. I'm struggling with how to accomplish this task. My initial attempt was to manually draw the triangle using the code below. <!DOCTYPE ht ...

CSS - Make the entire div clickable while leaving a specific area non-clickable

I have a block containing some information. Within this div is a hidden button labeled .remove. Clicking on the main block will take you to another page, while hovering over the div will reveal the button. However, clicking the button also navigates you aw ...

Display or conceal a div when hovering over dropdown menu choices

$('#dropdown-list').on('change',function(){ if( $(this).val()==="option 1" ){ $("#diva").hide() } else { $("#divb").show() } }); The code above is being used to hide or show a div based on a dropdown selection, which is f ...

Modify the text inside a div based on the navigation of another div

Hey there, experts! I must confess that I am an absolute beginner when it comes to JavaScript, JQuery, AJAX, and all the technical jargon. Despite my best efforts, I'm struggling to grasp the information I've found so far. What I really need is ...

Locate all span elements with a specific class using jQuery and conceal the parent divs further up in

I'm currently attempting to identify a specific span element with a particular class and then move up the DOM to find the closest div with another specified class in order to hide it. Is there something I might be overlooking? Any insights as to why ...

Creating a slick CSS dropdown menu with a slidedown effect

I recently designed a CSS menu with dropdown functionality, where the sub menu remains hidden until you hover over the menu item. I am curious to know if it is possible to implement a CSS transition to make the sub menu slide down instead. You can view a m ...

Aligning msform using CSS

Can someone assist me with centering the form (#msform, #msform fieldset) on all browsers while also keeping the image within the border and ensuring that the form does not extend beyond the right border? JSFiddle #msform { width: 1200px; margin: ...

Sending form data to PHP script following Javascript verification

After implementing basic validation for a contact form, I encountered an issue where the form would not submit to the PHP file responsible for sending emails. Despite setting the button type as "submit" and defining the form action as "contactform.php", th ...