Ways to make the sole element you've designed fade into view

I have a function that creates a note in the DOM. The issue is that when the note is created, it adds the "fade in" animation class to all notes instead of just the one being created. How can I modify it so that only the newly created note will have the animation effect?

function createNote(note){
    const noteContainer = document.querySelector("#note-display");
    let noteEl = `<div id="${note.id}" class="note fade-in">
                            <span onclick="removeNote(this)" class="remove-container">
                                <i class="fa fa-remove"></i>
                            </span>
                            <textarea onchange="updateMessage(this)">${note.message}</textarea>
                            <p>${note.date}</p>
                            <p>${note.time}</p>   
                    </div>`
    noteContainer.innerHTML += noteEl;
        console.log(noteContainer.innerHTML)
}

Answer №1

You might want to consider using the insertAdjacentHTML method, which can be found onMozilla MDN insertAdjacementHTML.

Your function could be structured like this:

function addNote(note){
    const noteContainer = document.querySelector("#note-display");
    let noteEl = `<div id="${note.id}" class="note fade-in">
                            <span onclick="removeNote(this)" class="remove-container">
                                <i class="fa fa-remove"></i>
                            </span>
                            <textarea onchange="updateMessage(this)">${note.message}</textarea>
                            <p>${note.date}</p>
                            <p>${note.time}</p>   
                    </div>`

    noteContainer.insertAdjacentHTML('afterend', noteEl);
}

Note that when using insertAdjacentHTML, it is important to escape any user input before inserting it as raw HTML. The documentation cautions:

It is crucial to sanitize user input before inserting it into a page with the insertAdjacentHTML() method to prevent XSS attacks.

For plain text insertion, it is better to utilize Node.textContent or Element.insertAdjacentText() instead of insertAdjacentHTML(). This way, the content is treated as raw text rather than HTML.

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

Vuetify 3 now displays full text in v-autocomplete without an ellipsis

Trying to truncate long text in a v-autocomplete component using Vuetify 3 and text-overflow: ellipsis, but it's not working. Check out the code below: <div id="app"> <v-app id="inspire"> <v-row align="cen ...

Using jQuery to implement AJAX file uploads

Currently, I am facing a challenge. I have multiple forms on a single page that are being submitted to the backend asynchronously via ajax. Some of these forms require a file upload without interrupting the process, so it needs to be handled asynchronousl ...

"Prisma vs. Supabase: A Comparison of Image Uploading

I am encountering an issue with adding image URLs to the Prisma database. I have successfully uploaded multiple images from an input file to Supabase storage, but when I try to add their URLs to the database, I receive an error regarding property compatibi ...

What are the methods for differentiating between a deliberate user click and a click triggered by JavaScript?

Social media platforms like Facebook and Twitter offer buttons such as Like and Follow to allow users to easily engage with content. For example, on Mashable.com, there is a Follow button that, when clicked, automatically makes the user follow Mashable&ap ...

Sending POST parameters via Jquery POST from a Java Servlet to Javascript

My JavaScript code initiates a POST request on my Servlet. $.post("RecipeServlet", { r_id: r_id, }, function(data, status){ var foo = data.foo; var bar = data.bar; }); Now, the Servlet is expected to work with the received r_id and then send ...

Add and remove input fields in real-time while also modifying nested arrays dynamically

Is it possible to dynamically add new input fields to an object within a nested array in React JS when the user clicks on a plus sign? I am looking to dynamically add and remove inputs. I am interested in adding and deleting propositionTimes dynamically u ...

Strapi: Enhancing User Experience with Unique Passwordless Customization Services

I have been attempting to modify the "passwordless" strapi plugin in order to generate verification codes consisting exclusively of digits. To achieve this, I need to override the createToken function within the plugin's service. Following the instru ...

My application built with React and Flask successfully processes JSON data on one route, but encounters issues on another route

The code I have in place is working quite well, with the frontend being the next area of focus. This code effectively registers a user and updates the database: export default class APIService { static RegisterUser(username, email, password, base_city, ...

create a PDF document that contains interactive input fields which can be modified using Angular

My goal is to generate an editable PDF using JavaScript and AngularJS, allowing users to input data into text fields on the document. However, when I download the PDF, the text fields are not editable. Here is a snippet of the code: var opt = { margin ...

JavaScript's sequential addition of nested arrays

Having trouble adding together 4 nested arrays in JavaScript. The arrays are structured like this: x = [[Array1(9)], [Array2(9)], [Array3(9)], [Array4(9)]] I am looking to "zip" them together and add the values. For example, I want to add Array1[0] with c ...

Vue: The function "priceFilter" is not defined in this context

function sanitizeInput(event) { event.target.value = event.target.value.replace(/[^\d.]/g, ""); event.target.value = event.target.value.replace(/^\./g, ""); event.target.value = event.target.value.replace(/\.{2,}/g, "."); event.targe ...

Ways to eliminate or conceal solely the play/pause symbol or button from the media player within the video tag

[Please see the screenshot attached, I am looking to remove the play/pause icon that is highlighted] [screenshot] How can I hide or remove only the play/pause button from the media player of a video tag? When I dynamically add the controls attribute using ...

There seems to be an issue with the CSV file, possibly indicating an error or the file may not be an SYLYK file when

After developing a node.js script to convert an array object into CSV format using the "objects-to-csv" library from NPM, I encountered an issue when opening the generated CSV file in WPS and Microsoft Office. The warning suggested that there was either an ...

Transforming JavaScript into TypeScript within an Angular 4 component class

Here is the Javascript code I currently have. I need to convert this into a component class within an Angular component. As far as I understand, the Character.prototype.placeAt() code is used to add a new method or attribute to an existing object. In this ...

Running Jasmine asynchronously in a SystemJS and TypeScript setup

I am currently executing Jasmine tests within a SystemJS and Typescript environment (essentially a plunk setup that is designed to be an Angular 2 testing platform). Jasmine is being deliberately utilized as a global library, rather than being imported vi ...

Dragging additional events into FullCalendar is disabled after applying a filter to external events

I am encountering an issue while attempting to drag events from the external events box to the fullcalendar. To replicate the problem, you can visit this CodePen: Initially, dragging an external event into the calendar works smoothly. However, after appl ...

Prevent sticky div from overlapping with footer

I currently have a social link menu that is fixed to the left side of my page, structured like this: <footer id="colophon"></footer> <div> <nav> <ul id="social"> <li>Link1</li> ...

Enhancing angular service with additional parameters

UPDATE: angular.extend and Object.assign both work fine, but which one is the better choice? I am facing an issue where adding more values to an angularjs service variable overwrites the previous value. Here is a sample code snippet: var testModule = ang ...

The CSS fullscreen background appears to function properly in Dev-tools, but fails to display correctly in an actual browser

I recently created a Meteor App/Website and I am encountering an issue with the fullscreen background image at the top of the page (). While the image displays correctly in Chrome Developer tools and Safari Responsive Design mode, it appears zoomed in on ...

Troubleshooting: Issues with JQuery and setTimeout() functionality

While using JQuery to monitor key presses for an HTML game, I encountered an issue where adding the jquery code to my gameloop resulted in an error message stating that keydown was not defined. <html> <head> //...Included JS files ...