Reputation: 27
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
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
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
Reputation: 447
Try this:
document.write("<div id='MainDiv' style='width:600px;margin:auto;'>")
Upvotes: 1