php:
$str1 = "AAA\r\nBBBB\\CCC";
echo"<textarea id='aa1' onfocus='erase(\"".$str1."\", \"aa1\");'></textarea>";
$str2 = "AAABBBB\\CCC";
echo"<textarea id='aa2' onfocus='erase(\"".$str2."\", \"aa2\");'></textarea>";
$str3 = "AAABBBB\CCC";
echo"<textarea id='aa3' onfocus='erase(\"".$str3."\", \"aa3\");'></textarea>";
$str4 = "AAABBBBCCC";
echo"<textarea id='aa4' onfocus='erase(\"".$str4."\", \"aa4\");'></textarea>";
javascript:
function erase(str, id)
{
alert("good");
var field = document.getElementById(id);
if(field.value == str)
{
field.value = '';
}
}
If I click on textarea id='aa1'
, nothing happens.
If I click on textarea id='aa2'
or textarea id='aa3'
, 'good' is printed but nothing happens to field.value
.
If I click on textarea id='aa4'
, 'good' is printed and field.value
is ''.
I want a string like AAA\r\nBBBB\\CCC
to work like textarea id='aa4
.
How can I do that?
I read the post below but it does not seem to help my situation:
Javascript Line Break Textarea
update:
I've replaced $str1
with json_encode($str1)
, So alert();
works fine now. (Thanks to Jordan Running.)
But the field.value
part still does not work.
code refactoring is too hard in my situation... Is there any way to handle the field.value
problem without code refactoring?
If quotes corrupt my HTML, I can putting $str1
in htmlspecialchars()
and displaying it in <textarea>
.