Pavan573
Pavan573

Reputation: 81

giving color to substring after javascript split()

i want to print the text entered in textarea with different colors means i am seperating the string with split() method it works fine then i want to print the substrings in textarea with colors how is it possible

<script type="text/javascript">
function init() {
    document.getElementById('txtarea2').focus();
}
function setcolor() {
var str=document.getElementById('txtarea2').value;
var str1=str.split(":");
var first= str1[0];
var second=str1[1];
document.getElementById('txtarea1').value= first + second;
document.getElementById('txtarea2').focus();
}
</script>
<body onload="init()">
<textarea id="txtarea1" rows="3" cols="20"></textarea>
<textarea id="txtarea2" rows="3" cols="20" onChange="setcolor()"></textarea>
</body>

please help me

Upvotes: 0

Views: 500

Answers (4)

Tejasva Dhyani
Tejasva Dhyani

Reputation: 1362

make an empty div and use it to append

<div id="newDiv"></div>

then create and append two different tags to to this div

first = '<a style="color:red">'+first+'</a>';
second = '<a style="color:blue">'+second+'</a>';
document.getElementById("newDiv").innerHTML=first+second;

Upvotes: 2

epascarello
epascarello

Reputation: 207537

You can not have different color text in a textarea.

You would need to use a rich editor to do that.

If the text is not editable, than use a div/pre to output it and color it with normal css tags.

Upvotes: 0

Ryan Kempt
Ryan Kempt

Reputation: 4209

As far as I know, it is not possible to have multiple colors in a single HTML textarea like you want.

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

Do you mean:


document.getElementById('txtarea1').style.color = 'red';

Upvotes: 0

Related Questions