2

Here is the running code on fiddle
You will see that It is working perfectly fine here but when I run this code in eclipse using glassfish server 3.2.1 in a xhtml page then it gives this error

javax.servlet.ServletException: Error Parsing /MasterPage/MiDASMaster.xhtml: Error Traced[line: 135] Open quote is expected for attribute "{1}" associated with an  element type  "class".


Here is the code of xhtml page(exactly same like fiddle) I tried it on Jsbin as well

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<style type="text/css">
.highlight {
    background:yellow;
}
.removeHighlight {
    background:green;
}

</style>
<script type="text/javascript" src="../Scripts/jquery-1.8.3.js"/>
</head>
<body>

<script type="text/javascript">

            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>';
        });


    }



</script>

<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>

..

1 Answer 1

3

Your XHTML is probably malformed.

Put your Javascript code into a CDATA section.

<script type="text/javascript">
    <![CDATA[
        alert("Your javascript here");
    ]]>
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

CData section? can you please elaborate
CData has removed the error but now the function test() is not calling
@viki that's a different problem and would be a different question. Your original problem is solved now. Feel free to ask another question once you have investigated some research in this new problem you are facing.
Sure :) I have posted my problem here please see
Can you please elaborate a little bit that why it was not working without CData.As I am new to JS so It would be really informative for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.