0
votes

I am receiving Uncaught SyntaxError: Unexpected token ILLEGAL in chrome with this code:

   var leftMiddleJS = 'function onmouseover() {
        document.getElementById("background").style.marginTop = "-135px"; 
        document.getElementById("background").style.marginLeft = "0px";
        document.getElementById("background").style.width = "760px";
        document.getElementById("background").style.height = "520px";
        document.getElementById("background").style.marginTop = "0";
        document.getElementById("background").style.marginLeft = "0";

        function click() { 
        document.getElementById("outsideContainer").style.marginTop = "0"; 
        document.getElementById("outsideContainer").style.marginLeft = "0"; 
        document.getElementById("outsideContainer").style.width = "300px"; 
        document.getElementById("outsideContainer").style.height = "250px"; 
        document.getElementById("insideContainer").style.marginTop = "-135px"; 
        document.getElementById("insideContainer").style.marginLeft = "0px"; }';

What is the problem with the code?

1
Its saying is on the first line - user2958098
Missing } after document.getElementById("background").style.marginLeft = "0"; - Michal Brašna
what are you doing with quote in function declration 'function onmouseover() - Suman Bogati
Don't do that. You should not store code in a string. - SLaks
Use ` \ ` to concatenate lines. - Teemu

1 Answers

1
votes

If you want to assign a string that spans multiple lines - use \ string continuation:

var leftMiddleJS = 'function onmouseover() { \
        document.getElementById("background").style.marginTop = "-135px";  \
        document.getElementById("background").style.marginLeft = "0px"; \
        document.getElementById("background").style.width = "760px"; \
        document.getElementById("background").style.height = "520px"; \
        document.getElementById("background").style.marginTop = "0"; \
        document.getElementById("background").style.marginLeft = "0"; \
\
        function click() { \
        document.getElementById("outsideContainer").style.marginTop = "0"; \
        document.getElementById("outsideContainer").style.marginLeft = "0"; \
        document.getElementById("outsideContainer").style.width = "300px"; \
        document.getElementById("outsideContainer").style.height = "250px"; \
        document.getElementById("insideContainer").style.marginTop = "-135px";  \
        document.getElementById("insideContainer").style.marginLeft = "0px"; }';