Member Avatar for feoperro


I have pasted my code below, please paste it into a JSP and tell me why it gives the error "Unterminated String Constant"

        <script language="javascript">
            function changeOn(object, objectId) {
                alert('in function');
                if (object == 'bookingsButton') {
                    alert('first if');
                    document.getElementById(objectId).src = 'Images/BookingsOn.png';
                } else if (object == 'detailsButton') {
                    alert('2nd if');
                    document.getElementById(objectId).src = 'Images/DetailsOn.png';
            function changeOff(object, objectId) {
                if (object == 'bookingsButton') {
                    document.getElementById(objectId).src = 'Images/Bookings.png';
                } else if (object == 'detailsButton'){
                    document.getElementById(objectId).src = 'Images/Details.png';
                    for (int i = 0; i < 2; i++) {
                    <a href="Bookings.jsp"><img src="Images/Bookings.png" id="bookings<%out.println("" + i);%>" onmouseover="changeOn('bookingsButton','bookings<%out.println("" + i);%>')" onmouseout="changeOff('bookingsButton','bookings<%out.println("" + i);%>')"/></a>
                    <a href="Details.jsp"><img src="Images/Details.png" id="details<%out.println("" + i);%>"/></a>
        <% }%>


Recommended Answers

All 2 Replies

The use of out.println() adds a newline character causing erroneous syntax of the generated output.
The generated html is,

<img src="Images/Bookings.png" id="bookings0
" onmouseover="changeOn('bookingsButton','bookings0
')" onmouseout="changeOff('bookingsButton','bookings0

There is a newline character after bookings0

Use out.print() instead. Which will generate html like,

<img src="Images/Bookings.png" id="bookings0" onmouseover="changeOn('bookingsButton','bookings0')" onmouseout="changeOff('bookingsButton','bookings0')"/>
Member Avatar for feoperro

You're a genius. Thanks a lot man.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.