bakesh
bakesh

Reputation: 27

Uncaught Syntax Error with document.write

document.write("<div id='MainDiv' style='width:600px;margin:auto;'>")

While opening the file in the browser an error occur like this

Uncaught Syntax Error: Unexpected token ILLEGAL

What is the solution for this?

Upvotes: 0

Views: 383

Answers (3)

Vasanth
Vasanth

Reputation: 210

$('body').html("<div id='MainDiv' style='width:600px;margin:auto'>");

Or:

$('body').append("<div id='MainDiv' style='width:600px;margin:auto'>");

Or in native JavaScript:

var myDiv = document.createElement('div');

This creates your div and you can style it now.

Upvotes: 0

deadboy
deadboy

Reputation: 859

You were missing the double quote at the end of the string. It's more code, but I suggest using the JS HTML/DOM API as it makes code much easier to maintain and read than just appending HTML strings.

var div = document.createElement("div");
div.id = "MainDiv";
div.style.width = "600px";
div.style.margin = "auto";
document.body.appendChild(div);

Upvotes: 0

zablotski
zablotski

Reputation: 447

Try this:

document.write("<div id='MainDiv' style='width:600px;margin:auto;'>")

Upvotes: 1

Related Questions