How can JavaScript be used to apply CSS formatting to text that appears as a new HTML element?

It's unclear if the question accurately reflects what I want to achieve. If I have a form that fades out and is then determined whether to display again through a cookie, can the confirmation message be styled using CSS?

For example, I would like to make the heading <h2>Success</h2> green. Is it possible to achieve this in my CSS file? Or is there another way?

$(function() {
    var completed = $.cookie( 'completed' ),
        form = $('#contactform'),
        msg = $('#contactf');
    if( ( completed != undefined ) && ( completed == 'done' ) ) {
        form.hide();
        msg.html( '<h2>Success</h2>Thankyou for submitting <b>the form.</b> We will get back to you as soon as possible.' ); 
    }

Edit: Specifically, give this a class name so I can style it in my CSS without affecting all h2 headings on my page.

Answer №1

To make your element stand out, apply a unique CSS style.

CSS

.highlight { background-color: yellow; color: black; }

JavaScript

 message.innerHTML = '<p class="highlight">Great job!</p>Thanks for filling out <strong>the survey</strong>. We appreciate your feedback.';

Answer №2

Alternatively, you can simply insert it directly into the code:

msg.html( '<h2 style="color:green">Success</h2>Thankyou for submitting <b>the form.</b> We will get back to you as soon as possible.' ); 

Answer №3

h2{color: green;}

You can apply this style if there is no h2 element on the webpage

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

Explore ways to incorporate special symbols in a jQuery array

I'm looking to include special characters in a jQuery array. I'm not quite sure how to do this. Currently, my code is: $scope.categories = ['Red', 'White', 'Rose', 'Sparkling']; and I would like it to be: ...

Customizing the appearance of individual columns in the Material-UI DataGrid

My goal is to center an IconButton within a DataGrid table, all of which are MUI components. Despite reading the documentation and trying various methods, I haven't achieved the desired result yet. In my latest attempt, I implemented the following: G ...

Is there a way to determine when all Angular commands have finished executing through Javascript?

I have been using the following code to determine when my webpage has finished loading, however, it seems like there might be an issue. It appears that the Angular components are not yet executed even when the document.readyState is complete: page.ope ...

Tips for displaying a React component with ReactDOM Render

_Header (cshtml) <div id="Help"></div> export default class Help { ReactDOM.render( <Help/>, document.getElementById('Help') ); } Help.js (component) } My objective is to di ...

Consolidate multiple generic items into a single entry

In my current project, I am structuring the types for a complex javascript module. One of the requirements is to handle multiple types using generics, as shown in the snippet below: export interface ModelState< FetchListPayload, FetchListR ...

Is it possible to create a clip plane in Three.js for objects that intersect?

Currently, my setup involves using a PlaneGeometry as the representation of water. I have also added a ship (gltf model) onto the water. The issue I'm encountering is that when the boat slightly overlaps with the water surface, the water is visible in ...

Tips for adjusting the font size of a Chip using Material-UI

I am using a widget called Chip const styles = { root:{ }, chip:{ margin: "2px", padding: "2px" } } const SmartTagChip = (props) =>{ const classes = useStyles(); return( <Chip style={{color:"white&q ...

What is the best way to apply the !important property in jQuery for CSS styling?

When setting the CSS property "padding-top" to a value stored in a variable (currentHeight), such as: jQuery("#page-container").css("padding-top", currentHeight); It functions correctly and produces: <div id="page-container" style="padding-top: 115px;" ...

What is the best way to make a recursive function display every return value in Javascript?

I need help with a function I have been working on. function main() { //retrieve the start text var start_text = document.getElementById("start_text").value; //retrieve target text var target_text = document.getElementById("target_text"). ...

What steps can be taken to ensure an animation does not continually reset to its original state when run infinitely?

I am looking to create an animation that continues infinitely without returning to its original state after completion, similar to a sun moving animation. Below is a sample project: .animator3{ -webkit-animation-name:j3; -webkit-animation-delay:0s; -web ...

Eliminating the final space in CSS flexbox layout

When I set the display of an element to flex, I noticed that the last space in a text string gets removed. <div class="has_flex"> Some text <a href="link">Link</a></div> After setting display to flex: <div class="has_flex"> ...

Browsers are failing to display any content during ajax calls

I am currently attempting to extract artist URLs from the following page: https://myspace.com/discover/artists?genreId=1002532 However, this page is making an AJAX call to retrieve user details. I have identified the URL in Firebug as: https://myspace.c ...

Maximizing the Use of Multiple Conditions for Styling in AngularJS

I have a table where I need to set different colors based on the values in one of the columns. I've successfully managed to set two colors using NgClass, but I'm unsure how to set up three different conditions. <scri ...

The click function for the parent div will not be executed if I click on the child div

Hey, I encountered an issue in my code that I need help with. $(document).ready(function() { $(document).on('click', '.rohit', function() { alert('rohit'); }) $(document).on('click', '.azad', ...

What is the best way to retrieve the value of a textbox in AngularJS?

Trying my hand at creating a basic web page using angular. I've got 2 textboxes and 2 buttons - one to set a predefined value in a textbox, and the other to add some text. Here's the code snippet: <!DOCTYPE html> <html lang="en" ng-app ...

What is the method for ensuring that the delete functionality is displayed within the same row?

In my division, there are options for names and deletion. It is displayed on my website as: 1.jpg delete The HTML code for the division is: <div id="files1" class="files"> <b class='dataname' >1.jpg</b> <span cla ...

Learn the best way to retrieve the highest number from a Array<String> in TypeScript or JavaScript

Can someone help me create a function in JS or TS that meets the following requirements? I am looking for a functional programming approach. ・Input type: Array(String) ・Output type: string or undefined Examples Input Result ["" ...

Experimenting with Angular using a template that includes a mat-toolbar

Currently, I am in the process of writing tests for my Angular application. Here is a snippet of the template used in my AppComponent: <mat-toolbar color="primary"> <span>CRUD Exercise</span> <span class="example-spa ...

Is there a way to obtain a user's screen size without relying on a physical device or measuring the diagonal?

I'm looking for a way to display something at its actual size on a browser. For instance, if something appears to be 20cm on the screen, I want it to truly measure up to 20cm. Some methods involve considering the diagonal resolution of the screen, li ...

Is there a way to execute PHP scripts using JQuery or AJAX without causing the page to refresh?

REFRESHED CODE I'm attempting to execute PHP scripts within my main PHP file where all content will be presented. My goal is to exhibit the outcomes from my PHP script including the SQL queries in use. I also aim to implement the feature of showcasin ...