How to color HTML elements based on a user command string
Posted
by Anonymous the Great
on Stack Overflow
See other posts from Stack Overflow
or by Anonymous the Great
Published on 2010-06-11T04:59:49Z
Indexed on
2010/06/11
5:02 UTC
Read the original article
Hit count: 288
When you type something like "red:Hi:" it will type "Hi" in red. The following script does not work and I do not know why, (The one who made the sorting PHP function is Graphain, thanks again!)
<?php
function getit($raw)
{
# If the value was posted
$raw = isset($raw) ? $raw : "";
# Split it based on ':'
$parsed = explode(':', $raw);
$colorClass = "";
$text = "";
if (count($parsed) >= 2)
{
$colorClass = $parsed[0];
$text = $parsed[1];
$text = "~~~" . $text . "~~~" . $colorClass;
return $text;
}
}
?>
<script type="text/javascript">
function postit()
{
var preview = document.getElementById("preview").value;
var submit = document.getElementById("post").value;
var text = <?php getit(submit); ?>
var t = text[0];
preview = t;
}
</script>
<textarea id="preview" cols=70 rows=5 readonly>Preview box</textarea>
<p>
<textarea id="post" cols=70 rows=5/>Submit box</textarea>
<p>
<input type="button" onclick="postit();" value="Submit"/>
© Stack Overflow or respective owner