Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define numbers in a html text with jquery

Tags:

html

jquery

I want to all numbers in my html text have different font-size with other character (or different font-family or different font-weight or ...). How to define numbers character? Is this possible with jquery? Can you help me?!

like image 872
mr.soroush Avatar asked Jul 18 '13 07:07

mr.soroush


People also ask

What is .VAL in jQuery?

jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.

How do I get text inside a div using jQuery?

To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.

How do I get inner text in jQuery?

Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.


2 Answers

Using regex you match the numbers an wrap them in a span with a class. No jquery needed.

    string.replace(/\d+/g,function(match){
    return "<span class='number'>" + match + "</span>"})

You can control the number design with css:

 .number{
   color:red;
   }

Demo

like image 141
raam86 Avatar answered Sep 21 '22 09:09

raam86


Since you asked for a JQuery solution, I transformed @raam86 's answer to jQuery, it looks like this:

$("div").html($("div").text().replace(/\d+/g, 
    function(m){ return "<span class='number'>" + m + "</span>" }));

Demo

like image 28
thmshd Avatar answered Sep 22 '22 09:09

thmshd