Reputation: 65
I am new to JavaScript. This is a fiddle with my code.
If I remove CDATA
then it works fine on fiddle but create issue on XHTML editors like eclipse:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
This is my JavaScript:
<![CDATA[
function test() {
alert(document.getElementById("divId"));
var regex = new RegExp('this',"gi");
document.getElementById("divId").innerHTML
= document.getElementById("divId").innerHTML.replace(regex,
function(matched)
{
return '<span class=\'highlight\'>' + matched + '</span>';
});
}
]]>
here is the <div>
in question:
<div id="divId">
This is the text This is the text This is the text This is the text
This is the text This is the text This is the the text
</div>
and I am unable to call test()
function. Any suggestions?
Upvotes: 2
Views: 774
Reputation: 234
Enclose between /* */ like this
/*<![CDATA[*/
function test(){
alert(document.getElementById("divId"));
var regex = new RegExp('this',"gi");
document.getElementById("divId").innerHTML
=document.getElementById("divId").innerHTML.replace(regex, function(matched)
{
return '<span class=\'highlight\'>' + matched + '</span>';
});
}
/*]]>*/
Upvotes: 3