Get text from a paragraph and change its color

search.lengthIs is there a way to get the text from a paragraph and change its color?

I tried:

<p id="userInput">1,2,3,4,5,6,7,some text,another text</p>

 var qq = document.getElementById("userInput").innerHTML.match(/5/g); 
 var blaaa = qq.style.color = "red";
 document.getElementById("userInput").innerHTML = blaaa;

      

EDIT:

var search = ['new', 'and'];
$(document).ready(function () {
    for(var i = 0;i<2;i++){
    $("div:contains('"+search[i]+"')").each(function () {
        var regex = new RegExp(search[i],'gi');
        $(this).html($(this).text().replace(regex, "<span class='red'>"+search[i]+"</span>"));
    });
    }
});

      

+3


source to share


1 answer


No, no, unless you wrap the text in your own element like span



var el = document.getElementById("userInput");

el.innerHTML = el.innerHTML.replace(/(5)/g, '<span style="color: red">$1</span>');
      

<p id="userInput">1,2,3,4,5,6,7,some text,another text</p>
      

Run codeHide result


+2


source







All Articles