Attempting to use jQuery on click to set the background image of a div to a base64 encoded data image

Check out what I'm working on here

The first div contains html with data:image;base64

<div id="large_photo_null" style="width:50px; height:50px; 
background-image: url( data:image;base64,R0lGODlhR..  );" >
</div>

When using html, the background image displays as expected.

Now, I am attempting to set a background image for another div. However, no changes are being made.

Here is the code:

<div id="large_photo" style="width:100px; height:150px;" >
This is meant to be a large photo where I expect to change the background.
</div>

<div class="thbn_photo" style="width:100px; height:150px;" >
This is like a thumbnail photo. Click here.
</div>

Here is the jQuery code:

$(document).on('click', '.thbn_photo', function(){
alert('thbn_photo clicked');

$('#large_photo').css( 'background-image: url( data:image;base64,R0lGODlhRgAzAJEAAMr..  );' );  

});

What could be going wrong? Is my code incorrect or is it not possible to set data:image;base64 using jQuery?

I found a similar topic that might help: Is there a way to set background-image as a base64 encoded image in javascript?

I will look into this further.

Answer №1

Finally, after spending 2 hours troubleshooting, I found a solution that worked perfectly for me.

const image = new Image();
image.src = "data:image/jpeg;charset=utf-8;base64, " + profilepic;
$("#profilepict").css("background-image", "url('" + image.src + "')");

Answer №2

When using jQuery's .css() method, you can pass arguments in the following format:

$('div').css({'color':'blue'});

Experiment with changing the structure of your last line to see if it affects the outcome.

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

Having trouble locating the search bar element on Walmart's website?

I'm currently working on a bot that needs Selenium to interact with the search bar on Walmart's website, where it will input the name of a specific product and perform a search. However, I've encountered an issue where no matter what method ...

Arrays cannot be used with $addFields in MongoDB

I have encountered a challenge where I am dealing with a field that can be either a string or an array. How can I handle this scenario in the $addField query? Below is my MongoDB query code snippet: db.ledger_scheme_logs.aggregate([ { $match ...

Tips on using Visual Studio Code to troubleshoot Angular 4 unit tests

I am working on an Angular 4 project with Material design in Visual Studio Code. The setup is done using angular/cli. Currently, I have been writing unit tests using Karma and Jasmine. However, when trying to debug the tests by setting breakpoints, it doe ...

Prepare yourself for the possibility of receiving a caution while within a try-catch block

After implementing the MongoClient.connect method and encountering the warning 'await' has no effect on the type of this expression, it became clear that including the .catch line immediately following (which is currently commented out) mitigated ...

How to trigger the submission of a form within the submission event of another form using jQuery

I am encountering an issue with having two forms on a single page. Whenever I click on the submit button of the parent form, the parent form's submit event is triggered. In this event, I have included logic to submit the second form using the submit m ...

Error: The function Undefined is not recognized by express.io

Struggling to configure express.io with individual files for each route? Check out the examples provided. Currently, I have a typical Express setup that I want to transition to express.io: Project app.js routes servepage.js Views ...

Increase and decrease a counter in Javascript by clicking on two separate image tags, with increments and decrements between 1 and 10

Hello! I really appreciate your help. I am currently facing the following issue: <div id="thumb1"> <img class="img-responsive" id="prova" src="img/thumb-up-dark.png" alt=""> <div id="pinline">0</div> ...

Which internal function is triggered in JavaScript when I retrieve the value of an array element by its index?

Check out this fascinating wtfjs code snippet: var a = [,]; alert(a.indexOf(a[0])); This example highlights the difference between uninitialized and undefined values: The array a contains only one uninitialized element. Accessing a[0] returns undefined ...

Error: The API_URL_KEY variable has not been declared

hardhat.config.js require("@nomicfoundation/hardhat-toolbox"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.18", }; /* @type import('hardhat/config').HardhatUserConfig* ...

What causes an AJAX POST request to fail?

While working on a simple HTML page with a form, I encountered an issue with my POST request failing without any response from the server. Can someone please help me figure out what I'm doing wrong? function createRequest(url, body) { var respons ...

Unexpected display issues with CSS shapes in Bootstrap framework

Been experimenting with creating custom shapes in Bootstrap - they display correctly in HTML and CSS, but Bootstrap distorts them. It seems like there are no conflicting classes. Could it be necessary to use '!important' on some of the elements? ...

What is the correct way to horizontally center a Material icon within a div?

How do I correctly center a Material Design icon within a div to fix the gap issue? I have tried using text-align on the items div, but it hasn't worked. (Refer to screenshots for clarification) @import url('https://fonts.googleapis.com/css2? ...

"Uncovering the ways iOS disrupts background-size functionality

I am currently working on creating several marked images that will adjust in size to fit the length of their content. This functionality works well on most browsers, except for those on iOS devices like iPhone and iPad. Take a look at the image-link below ...

Disable the upload functionality for the item in Bootstrap Fileinput

I'm having trouble removing the upload icon from the item box. My goal is to automatically upload all images when I click on the "send" button. Thank you https://i.sstatic.net/3rA4G.png var btns = '<input type="hidden" id= "<?= $csrf[&apo ...

While using the Android Firefox browser, I noticed that the keyboard is causing my screen to shrink in size

As I work on creating a cross-platform HTML page, I have encountered an issue with text fields on Android smartphones. When I attempt to type in a text box, the keyboard pops up and causes my HTML page to shrink within the viewport. I'm curious to kn ...

Tips for displaying specific HTML elements in AngularJS using ng-repeat and ng-if

I am working with some bootstrap HTML code that includes an ng-repeat function. <div class="row"> <div class="col-lg-4" ng-repeat="p in persons"> <img class="img-circle" src="images/{{ p.image }}" width="140" height="140"> < ...

The latest value has replaced the state's previous value

In my current function, I am updating an array stored in state based on the status of 9 tables. Each table calls this function to check its status, and if true, it is added to the array. const [projectSpaceTypeWarning, setProjectSpaceTypeWarning] = useSta ...

Discover the power of Vue.js 3 by seamlessly integrating anonymous functions within event handlers

Is it possible to invoke an anonymous function within an event handler in Vue.js 3? I came across several options on various websites: <button @click="return function() { console.log('test')}()">Click me</button> <butto ...

The dividers flicker in and out of view

I have a menu with 7 elements, where clicking on an element causes its content to appear by fading in. If another element is clicked, the current content fades out and the new content fades in. I've successfully implemented this concept for 3 of the 7 ...

Struggling to integrate the navigation bar with Wordpress platform

For a while now, I have been facing challenges with the navigation bar on the WordPress theme I am currently creating. I am struggling to get the navigation bar to display correctly. Below is the code snippet from my header.php file: <body> &l ...