What is the best way to conceal a broken image icon without sacrificing all of the stylesheet?

Can someone assist me in understanding and modifying this code?

Here is the code snippet:

<table>
      <tr>
          <td>
              <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
              <input type="file" name="pathheader" id="pathheader"  class="form-control-file" aria-describedby="fileHelp" style="display:none;">
          </td>
          <td>
              <img src="" id="img2" class="img2" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
              <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="fileHelp" style="display:none;"><br>
          </td>
      </tr>
      <tr>
          <td>
              <input type="button" name="" value="Seleccionar header" id="browse_file" class="btn btn-danger form-control">
          </td>
          <td>
              <input type="button" name="" value="Seleccionar home" id="browse_file2" class="btn btn-danger form-control">
          </td>
      </tr>
</table>

Additionally, here is the accompanying JavaScript code:

$("#browse_file").on('click',function(e){
        $("#pathheader").click();
    })
    $("#browse_file2").on('click',function(e){
        $("#pathhome").click();
    })
    $("#pathheader").on('change',function(e){
        var fileInput=this;
        if (fileInput.files[0])
            {
                var reader=new FileReader();
                reader.onload=function(e)
                {
                    $("#img").attr('src',e.target.result);
                }
                reader.readAsDataURL(fileInput.files[0]);
            }
    })
    $("#pathhome").on('change',function(e){
        var fileInput=this;
        if (fileInput.files[0])
            {
                var reader=new FileReader();
                reader.onload=function(e)
                {
                    $("#img2").attr('src',e.target.result);
                }
                reader.readAsDataURL(fileInput.files[0]);
            }
    })

I have seen a solution using Ajax to hide the icon image while keeping the background and border styles intact. Can anyone guide me on how to implement this with Ajax or even with CSS?

Your help will be greatly appreciated. Thank you.

Answer №1

If you encounter an error, one solution could be to insert a transparent base64-image into your code.

onerror="this.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='"

Responding to a query from the original poster:

Base64 is a format that allows you to embed images directly in your HTML file rather than linking to an external source.

Let's explore the image src:

First:

<img src="data:image/png;base64...

This informs the browser on how to interpret the following code. It specifies that the MIME-Type (File Type) of the src is .png and that it is encoded in base64, essentially representing image data as a string.

Following that is the actual Data:

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=

These characters symbolize image data like pixels, colors, and alpha values. For a deeper understanding of this process, refer to https://www.rfc-editor.org/rfc/rfc3548 or check out the links under Further Reading.

Further Reading

  • Embedding Base64 Images (Stackoverflow)

  • Displaying Base64 images in HTML (Stackoverflow)

  • Base64 image converter

  • Base64 on Wikipedia

Answer №2

The issue arises when you combine a class and an img tag together. By assigning a class inside the image tag and then referencing a background image in that class, it may result in a broken image being displayed. This is because it will be referring to two image tags simultaneously. The solution is to use a div instead of an img tag, which will prevent the broken image icon from appearing and allow the background image to display as intended.

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

Perform an AJAX request to an encrypted URL with an unverified certificate

I'm experiencing an issue with my site that makes AJAX JSONP calls to a secured (SSL) web server. Everything works smoothly when using an unsecured (HTTP) web server, but once I switch to the SSL version, the call never returns. After checking with Fi ...

You are unable to define a module within an NgModule since it is not included in the current Angular compilation

Something strange is happening here as I am encountering an error message stating 'Cannot declare 'FormsModule' in an NgModule as it's not a part of the current compilation' when trying to import 'FormsModule' and 'R ...

A guide to resolving cross-origin resource sharing issues using a reverse proxy

After creating a JavaScript web application for processing documents, I am now looking to integrate with web services like NLTK-server, TIKA-server, and SOLR for further analysis. While I can successfully access the REST endpoints of these services using c ...

Search for a DIV element within iMacro on a consistent basis

I recently started using iMacro and encountered an issue while recording a script that involved clicking on a pop-up when it appeared on the screen. The problem arose because the pop-up only appears when a new event is posted. Therefore, when I initially c ...

Deciphering the principles of Bootstrap

I'm looking to understand more about what Bootstrap is. I know it's commonly used for styling web pages, but could you provide me with a clear explanation? Can big companies like Twitter, Facebook, or YouTube utilize Bootstrap for their platforms ...

Binding hover and load events using jQuery on elements that are dynamically generated

One should note that the click event can be successfully bound to an element with the class name keybox, even if this element is dynamically generated. The code for this would look like: $('body').on('click', '.keybox', funct ...

Exploring the Relative Positioning of Two div Elements

Can anyone assist me with finding and comparing the positions of two '<div>' tags using an if statement? I stumbled upon a suggestion in my research, which goes like this: var obstacle = $('#obstacle').css('left', &apo ...

Leveraging composer to enhance the organization of my dependencies

As I organize my files for my PHP-based website, I've turned to composer for assistance. One issue I've encountered is loading front-end dependencies like jquery into my pages, specifically the index.php. In the past, I manually specified each js ...

utilizing variables in the rel attribute within jQuery along with the $.get method

Can someone help me with this issue: I am trying to hide all submenus on a page except for the one related to the current loaded page. The information about which submenu should remain open is stored in a PHP session, so I attempted the following: $.get( ...

When using Vue.js, binding may not function properly if it is updated using jQuery

Link to JsFiddle Below is the HTML code: <div id="testVue"> <input id="test" v-model="testModel"/> <button @click="clickMe()">Click me</button> <button @click="showValue()">Show value</button> </div& ...

HTML replacement on an element cannot be done more than once. Solving the issue with Rails and JQuery Ajax

I have created an application with 4 buttons, each representing a different directory. When a button is clicked, an Ajax call is made to generate a JStree element displaying the directory hierarchy. Everything works perfectly when I click on the first but ...

Issues encountered while sending HTML Form data to MySQL with Node JS

I have been experimenting with a basic html form to mysql using nodejs, but unfortunately it is not functioning as expected. The HTML file is named index.html and the Node.js file is called test.js. Below you can find my code: My HTML <!DOCTYPE html&g ...

Steps for including AjaxControlToolkit in a Visual Studio 2013 web-forms template?

It seems like the project I'm working on is already quite complex, and I'm not sure if I can handle it alone. There are a lot of components included in the project from the start, and I'm finding it difficult to understand their purpose. I ...

Struggling with a filtering and sorting problem in my code, looking for some assistance

I'm encountering an issue with an isotope filter menu. The goal is for it to load data based on a timestamp and allow filtering. However, there seems to be a conflict between my sortBy script and the filter script. Below is the code in question: Sor ...

Tips for maintaining knowledge after redirecting to a new page

Building an app using Ionic 4 where I need to display vouchers from a database as images. Each image should act as a link to a details page showing more information about that specific voucher. However, I am struggling to figure out how to keep track of th ...

Align navigation bar using Angular Material

Trying to center my navbar has been a struggle for me. I attempted using 'layout-align='space-between center' but it's not taking up 100% width and is acting more like an inline element. I've been following the documentation close ...

The jQuery AJAX call is successful in Firefox, but unfortunately, it is not working in Internet Explorer

I've encountered a perplexing issue with an AJAX call. It's functioning perfectly fine in Firefox, but for some reason, it's not working in IE. Curiously, when I include an alert() specifically for IE, I can see the returned content, but the ...

Is there a way to sync real-time database updates with the website?

I'm new to web programming and have a python program that collects keyword specific tweets from Twitter and adds them to a MySQL database. I want to display these tweets on a website in real-time. Should I have Python send the tweet text directly to t ...

Choosing an option on a WordPress admin page may vary depending on the selection of another

I am facing a challenge with my code that involves two selections: category and type. The type selection should be dependent on the category selected from the database. Here is a snippet of my code: HTML $ps_type_table_name = $wpdb->prefix . 'ps_ ...

Sorting tables using ajax-generated data

I am having trouble with a table that is generated using ajax (PHP). Even though I have included all the necessary attributes for tablesorter, it doesn't seem to be working. Can someone please point out where I might have made a mistake? Any suggestio ...