jbb
jbb

Reputation: 79

How to change a color of a piece of text in jQuery?

I use jQuery to change h1 header after end of a game and I have the following line of code:

$('h1').text(winningPlayer+" has won!").css("fontSize", "55px");

The question is, how can I set a different color just for winningPlayer part of text??

Upvotes: 1

Views: 1288

Answers (1)

Maheer Ali
Maheer Ali

Reputation: 36594

Wrap the winningPlayer in <span> and change .text() to .html(). Below is the demo

let winningPlayer = "John"
$('h1').html(`<span style="color:blue">${winningPlayer}</span> has won!`).css("fontSize", "55px");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1></h1>

Upvotes: 6

Related Questions