Ways to incorporate additional CSS after an image has been uploaded

I'm having an issue with the text box not completely staying inside the gray area when I upload an image. Is there a way to automatically expand the gray area where the text box is located when an image is uploaded? Thank you!

Check out the code on Jsfiddle

let cssProps = {
  'font-weight': '',
  'font-style': '',
  'text-decoration': ''
}

$('input[name="textStyle"]').change(function() {
  const val = $(this).val()
  if ($(this).is(':checked')) {
    switch (val) {
      case 'bold':
        cssProps['font-weight'] = 'bold';
        break;
      case 'italic':
        cssProps['font-style'] = 'italic';
        break;
      case 'underline':
        cssProps['text-decoration'] = 'underline';
        break;
    }
  } else {
    switch (val) {
      case 'bold':
        cssProps['font-weight'] = '';
        break;
      case 'italic':
        cssProps['font-style'] = '';
        break;
      case 'underline':
        cssProps['text-decoration'] = '';
        break;

    }
  }
  $('textarea[name="styledText"]').css(cssProps)
});
var loadFile = function(event) {
   var image = document.getElementById('output');
   image.src = URL.createObjectURL(event.target.files[0]);
};

Answer №1

To achieve this, you can utilize a `load` event listener and then add a specific class to the element in order to style it accordingly:

image.addEventListener('load', () => {
  image.classList.add('loaded');
});

For example, consider the following implementation:

const url = 'https://cdn.pixabay.com/photo/2021/04/02/23/33/annas-hummingbird-6146187_960_720.jpg';
const image = document.getElementById('image');
image.addEventListener('load', () => {
  image.classList.add('loaded');
})
image.src = url;
img.loaded {
  border: 5px solid red;
}
<img id='image' width='200'/>

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

Animated CSS sidemenu (utilized as a filtering panel for a table)

Hi there, I'm having some trouble with CSS Animation. I recently started developing websites and am using Bootstrap 4 along with Animate.css for animations. My goal is to have an icon button expand sideways to reveal a div containing select elements f ...

Tips for utilizing comment tags efficiently

I have encountered an issue with IE7 where my CSS is not functioning properly. In an attempt to target IE7 and lower versions specifically, I inserted IE comment tags into the HTML code. However, despite my efforts, it does not seem to be effective. Initia ...

The PurgeCSS CLI does not generate CSS files beyond the command line interface

I'm struggling to successfully extract my CSS using purgecss from the command line and save it to a separate file. The instructions on PurgeCSS.com are vague: By default, the CLI only outputs the result in the console. To save the purified CSS files ...

What is the best way to retrieve data from a child component using v-if in Vue.js?

Having trouble accessing another variable within a Vue for loop... <div class="list-group" v-for="index in max_slots"> <div v-if="user[index].firstname"> <a href="#">{{user[index].firstn ...

Using XSLT to convert HTML into the desired text format

It has been almost two decades since my last encounter with XSLT. I am attempting to transform documents, like the HTML snippet below, into the desired output. <p> おばあさんは、とても <ruby><rb>喜</rb><rp>(</rp ...

Align the child element to the baseline and have it aligned to the right within an `<li>` list item

I am interested in aligning the price of coffee to the right end of the coffee name. For example, the price 1.80 should be in line with Americano and 10.00 should be in line with Macchiato. https://i.sstatic.net/unm26.jpg ul { list-style: none; pad ...

Enable the expansion of a div by dragging and dropping an image onto it

Take a look at this fiddle. In this fiddle, I am able to drag and drop images onto the .drop-zone. However, I would like to enhance it so that when I drag an image onto it, the .drop-zone div expands to where I place the image. It's okay if the expand ...

No response received from Node server following successful data insertion into MongoDB

I recently tried to insert data into MongoDB using Node.js, but unfortunately I am not receiving the expected response from the server. Instead of getting a message like Data has been saved, I encountered some errors. Below is the code snippet I used: con ...

Mistakes in serializing arrays for javascript in an ajax request

I am trying to send an array of JavaScript objects in an Ajax call. This is how I create my array: var itemsToEdit = []; $(".editedItem[value='true']").closest("tr").each(function() { var itemToEdit = { id: $(this).find(".i ...

Ready to learn how to capture user input from a form and then send that data via email using an ajax request

I'm currently in the process of setting up a straightforward form that allows users to enter their email address to subscribe to our newsletter. Once submitted, I should receive an email containing the user's email address. Here’s what I have ...

Employing an odd/even toggle for assigning class names

I need each this.state.title to be aligned based on a different classname. I attempted using css flex boxes/nth-of-type/nth-child, but it didn't work well with React. I'm utilizing this.state to access my objects. My failed strategy render: f ...

Using ASP.net MVC 4 to Implement Validation with Bootstrap Modal and PartialView

After switching from a simple View with validation to using a bootstrap modal and PartialView in my application, I encountered some issues. The client-side validation no longer works and the server-side validation redirects me to a new page instead of disp ...

What is the best way to replicate text in ReactJS?

I need some help with implementing a copy to clipboard feature in ReactJS. My goal is to have text copied to the clipboard when a user clicks on a link. My current setup involves using Chrome 52 and I am only focusing on supporting this browser at the mom ...

Adding a query parameter to a dynamic route in NextJS

In my NextJS application, I have implemented a language selector that is displayed on every page. The goal is to change the current URL by adding a query parameter lang=en when a new language is selected. The function responsible for updating the URL look ...

What is the best way to properly format letters with accents (such as French letters)?

I have encountered a challenge where I created a PHP file to display French text and then utilized this text in an AJAX file (specifically the responseText). The issue arises when trying to show the French responseText in an alert using JavaScript, as ac ...

Is it possible to use require() to read a .json file from a dependent library?

Is it possible to access a .json file within one of my dependent libraries? I'm hesitant to use fs to read ./node_modules/somelib/properties.json because the library could have been installed globally. Is there a way to achieve this using require in ...

The previous callback function is behaving strangely

<div class="actions_container" id="message_form_container"> ...<input type="text" id="message" />... </div> <div class="actions_container" id="coffee_form_container"> ...<input type="text" id="coffee_message" />... </div> ...

Execute JavaScript code once all the AngularJS template directives are fully loaded

On my HTML page, I am utilizing AngularJS template directives. Here is an example of how it looks: <div class="row"> <div class="col-sm-12"> <div logo></div> <div login-card></div> ...

Is there a way to properly structure the json data displayed in my network tab on Chrome?

After sending an http request to my backend, I received a json response in the network tab. However, the format of the json is unreadable. To clarify, here is a screenshot: https://i.stack.imgur.com/RBiTd.png Currently using Chrome, I am seeking assistanc ...

Having trouble retrieving input data from a modal pop up with Angular.js

I am facing an issue with my modal pop up form in Angular.js. I am unable to retrieve the form data using ng-model. Below is the code snippet: <modal title="Owner Information" visible="showModal"> <form class="ng-pristine ng-valid" id="frmsignu ...