user3148503
user3148503

Reputation: 33

Setting the color of text in a textarea on the go

I am learning JavaScript and testing things. I have made a text-area which takes some html code and prints the result out.Its working well but I want that whenever a tag is typed like <h1> the color should change from white to red. This is my code which does not do so, Please help me in fixing it ..

Code:

<script>
    $("#_co").on("input propertychange", function(){
        $("#out").html($("#_co").val());
         var text = jQuery("#_co").val();
        if (text.contains("<")){
            text.css('color','red');
        }
    }); </script>

Upvotes: 0

Views: 76

Answers (2)

mahendra rajeshirke
mahendra rajeshirke

Reputation: 122

There no direct way to achieve the same, but this would help you work around

jsfiddle

Pls have a look

function divClicked() {
var divHtml = $(this).html();
var editableText = $("<textarea />");
editableText.val(divHtml);
$(this).replaceWith(editableText);
editableText.focus();
// setup the blur event for this new textarea
editableText.blur(editableTextBlurred);
}

function editableTextBlurred() {
var html = $(this).val();
var viewableText = $("<div>");
viewableText.html(html);
$(this).replaceWith(viewableText);
// setup the click event for this new div
viewableText.click(divClicked);
}

$(document).ready(function() {
$("div.div").click(divClicked);
});  

Upvotes: 1

Steven
Steven

Reputation: 1494

Something like this one below?

Fiddle

$("#dummy").blur(function(){
    var color = $(this).val()=='' ? 'red' : 'white';
     $(this).css('background-color', color);
});

Upvotes: 0

Related Questions