0

I have a dynamic value let say var x= "123456789"; now I want to apply the bold css property to last three no i.e 789 in above string. I tried by breaking the string to array, I was successful to some extent but then I struck on how should I show remaining no i.e unchanged part i.e. 123456.

Please guide me how can I approach to this or what's the best solution for this.

Output should be like 123456789. This string can be dynamic.

4
  • 1
    Why don't you add the code you've tried to your question. Your question isn't very clear at the moment, and seeing what you've tried might clarify things. Also that's an integer, not a string. Commented Apr 25, 2021 at 12:48
  • @Andy updated it to string. Commented Apr 25, 2021 at 12:49
  • You can put the sub string in some span tag with an ID/Class and then apply a css only on the given ID/Class. Commented Apr 25, 2021 at 12:52
  • @ValeriF21 I had done that but then it's works like 12345678 and again bold 678. Instead it should be 12345678 only once with bold part. So I struck that how can I remove the last 3 digit from original no then? Commented Apr 25, 2021 at 12:55

4 Answers 4

3
let str = "12345678" //Given string
str.slice(-3) //Will give you "678"
str.slice(0, str.length - 3) //Will give you "12345" - i.e. Everything except the last 3 characters.
    

So if after you have done the first operation the "678" is showed twice (In regular and bold) then apply the second operation to slice the "678" from the string. Hope it helps if not you can add your code and i can show you again.

Sign up to request clarification or add additional context in comments.

Comments

2
let num = "123456789";
num = `${num.slice(0, -3)}<b>${num.slice(-3)}</b>`; // Will give you string: "123456<b>789</b>"

So you can use the slice function and use string interpolation to enter the bold tags. This would make the last 3 characters of the string bold.

Comments

1

You can try this:

<html>
<body>
<p id = "p1"> </p>
<script>
    var x = "123456789";
    var last3 = x.substr(-3,3); //last 3 digits
    var newx = x.replace(last3, last3.bold()); //new string by replacing last 3 digits with bold
    document.getElementById("p1").innerHTML = newx;
</script>
</body>
</html>

Comments

0

just improved a little

<div id= "display"></div>

<script>

let x = "12345678";
let second = str.slice(-3); // last 3
let first = str.slice(0, str.length - 3); // first part

// getting the display div

const display = document.querySelector("#display");


//inserting to display div
display.innerHTML = `${first}<strong>${second}</strong>`;

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.