javascript - JQuery if line of text starts with character, wrap whole line in span -


so have text , need give different color lines start character. have far this:

var msgtext = $(".msginfo").html();  var textlines = msgtext.split("<br>");  $(textlines).each(function(line, text) {    if (text.indexof("&gt;&gt;") == 0) {      $(".msginfo").html(function(i, html) {        return html.replace(text, "<span style=\"color:#bfbfbf;\">" + text + "</span>");      });    }  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="msginfo">nova mensagem 2<br><br>cumpts;<br>josue<br>apt 253<br><br>&gt;&gt;nova mensagem 1<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt;&gt;&gt;como sei para que enviei mensagem? preciso de um campo que diga para quem foi mensagem?<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;&gt;&gt;mensagem    de teste para verificar o nome de quem enviou o email.<br>&gt;&gt;&gt;&gt;&gt;&gt;<br>&gt;&gt;&gt;&gt;<br>&gt;&gt;<br></div>

but it's not wrapping lines. ones text other ">>" in them.

what doing wrong?

thanks

that replacing seems bit nasty , costly. how like:

var msgtext = $(".viewmessage .msg .msginfo").html(); var newtext = ''; var textlines = msgtext.split("<br>"); $(textlines).each(function(line,text){     if (text.indexof("&gt;&gt;") == 0){         if (text.indexof("&gt;&gt;") == 0){         text = "<span style=\"color:#bfbfbf;\">" + text +"</span>";     }      newtext += text; }); $(".viewmessage .msg .msginfo").html(newtext); 

be careful though. epascarello pointed out, jquery selector $(".viewmessage .msg .msginfo") select multiple elements. should make sure grabs single element or "textreplace" of found elements if desired.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -