Change the color of the final two letters in the word within h1

There is one h1 heading in my code, and it looks like this:

HTML

<h1>AwesoME</h1>

CSS

h1 {
color:#eee
}

h1:last-word {
color:#000
}

I want to only change the last two characters of the h1 text. Is it possible to achieve this using JavaScript or jQuery?

Answer №1

Check out this jQuery solution:

$('h1').html(function() {
  var text = $(this).text();
  return text.substr(0, text.length-2)          //keep all but the last two characters
         + '<span>'+text.slice(-2)+'</span>';  //wrap last two characters in a span tag
});
h1 {
  color: #eee;
}

h1 span {     /* custom style for span */
  color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>AwesoME</h1>


For those who prefer vanilla JavaScript, here's an alternative solution:

var heading = document.querySelector('h1'),
    text = heading.textContent;

heading.innerHTML = text.substr(0, text.length-2)          
              + '<span>'+text.slice(-2)+'</span>';  
h1 {
  color: #eee;
}

h1 span {     
  color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>AwesoME</h1>

Answer №2

Consider implementing css :after

p {
  font-size: 16px;
}
p::after {
  content: "!";
  position: absolute;
  color: blue;
  left:10px;
}
<p>Hello World</p>

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

Show and hide menu items without automatically scrolling the user back to the top of the page

I am currently working on a project where I have an image button that toggles between expanding and collapsing a menu using JavaScript. The issue I am facing is that every time the button is clicked, it takes the user back to the top of the page. My goal ...

ESLint encountered an error while attempting to load the configuration "next/babel" for extension

Every time I try to generate a build of my Next.js app by running "npm run build," I keep encountering this error. Check out the error I'm getting while running npm run build here. Also, take a look at my .eslintrc.json file here and my .babelrc file ...

Looking to incorporate multiple accordion drop down menus on your website? Utilize a combination of HTML, CSS, and JavaScript to

I am experiencing a challenge with implementing multiple accordion menus on my website. Whenever I attempt to duplicate the code, the new accordion menu appears but clicking on the first bar simply scrolls me back to the top of the webpage. Below is the H ...

Is there a way to programmatically scan through all directories and include only files with a .js extension into my Discord collection?

I'm currently working on the following code and I'm looking for a way to adjust it in order to check each sub-directory: const fs = require('fs') module.exports = (client, Discord) =>{ const command_files = fs.readdirSync(' ...

Angular Material's md-checkbox is a required component

I am working on a form that consists of checkboxes representing the days of the week. When the user hits submit without selecting any checkboxes, I want an error message to appear. Here is the HTML code snippet that I have: <form id="addEditForm" nam ...

Editing content directly within an ASP.NET Core MVC application through AJAX encounters a 400 error, indicating a Bad Request

I'm attempting to implement inline editing using AJAX in ASP.NET Core MVC, but I keep receiving a 400 error (Bad Request). The idea is to click on a table row to edit and save the new values in an SQL database. Could someone confirm if I am on the r ...

API for Product Hunting

I am currently exploring the possibility of utilizing ProductHunt's API to perform basic get requests. The API mandates a token and redirect URI for access. However, when attempting to retrieve posts by applying the provided ApiKey and URI, I find mys ...

Having trouble with importing a TypeScript class: encountering a "cannot resolve" error message

Could you lend me your expertise? I'm puzzled by this issue that seems to be quite simple and straightforward: export class Rectangle { height: number = 0 width: number = 0 constructor(height: number, width: number) { this. ...

Experimenting with ngModel in a Jasmine test suite

I've created a directive that generates input fields and connects them to the scope using ngModel. When a user uses this directive in the HTML file like so: <div tk-quick-form="formStructure" form-data="formData" id="main_form" ></div> a ...

Using Three.js to apply a sprite as a texture to a spherical object

function generateLabel(icon, labelText, labelText2, labelText3, bgColor, fontColor, transparency, xCoordinate, yCoordinate, zCoordinate){ $('#imglabelcontainer').append('<div class="imglabel" id="imglabel'+labelText+'" style ...

Unable to trigger event on Bootstrap 4 calendar

I have integrated the dateTimePicker functionality into my project. Below is the HTML code snippet I am using: <div class="col-sm-3"> <div class="form-group"> <label for="sdating_1"><i class="fa fa-calendar" aria-hidden=" ...

Experiencing difficulties installing the MEAN stack

I've been attempting to set up the MEAN stack by following a tutorial on Bossable website. I'm using Webstorm and MongoDB for this installation. Unfortunately, I'm facing some issues and encountering errors. Every time I try to connect to l ...

The post function is causing an issue and displaying an error

I am currently working on a test application that is based on the tutorial found at https://docs.angularjs.org/tutorial/step_00. The app is functioning well, however, I am encountering an issue with the post method. index.html ... <div class="control_ ...

Step-by-step guide on resolving AngularJS Issue: [$injector:modulerr]

I'm encountering an issue with Angular JS where I'm receiving the following error: jquery.js:7993 Uncaught Error: [$injector:modulerr] Failed to instantiate module application due to: Error: [$injector:nomod] Module 'application' is no ...

Modify the color of the chosen option in the dropdown menu without affecting the rest of the options

I am facing an issue with dynamic drop down lists in my HTML content. The requirement is to change the color of the selected option to dark red if the option is "success". I have implemented a jQuery function for this purpose. HTML: <select class="rea ...

A guide on changing a plus sign into a minus sign with CSS transition

Is there a way to create a toggle button that changes from a plus sign to a minus sign using only CSS, without the need for pseudo-elements? The desired effect is to have the vertical line in the "+" sign shrink into the horizontal line. While I know it& ...

CSS animation is supported on most browsers with the exception of Safari

I'm facing an issue with my animation that works fine on all browsers except Safari. I've tried using the autoprefix extension in VSCode to automatically add CSS vendor prefixes, but the animation still isn't functioning correctly. Here&apo ...

Is it possible to use JavaScript to manage multiple instances of the same form on a single webpage?

In my thumb gallery, I have implemented a functionality using ajax/javascript to seamlessly submit a form for each image to report it as broken. This form and script are templated, with the script located in the header and the form printed multiple times o ...

Ways to retrieve the <select> value in AngularJS

Having trouble passing the selected value from my tab to my $scope object. Here's the snippet of my HTML code. <div ng-controller="RouteController"> <select ng-model="selector" ng-options="opt as opt.label for opt in options"> ...

Sharing data between a Javascript applet and a webpage: strategies for sending results from the applet back to

When it comes to calling a method in an applet from JavaScript, I am facing a challenge. Both the applet and the JavaScript code are running on the same webpage. I am aware of how to call applet methods from JavaScript and vice versa using techniques like ...