Reputation: 15034
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>css demo</title>
</head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
(function(){
var html = '<p>';
html += 'Hello World';
html += '</p>';
$(document.body).innerHTML = html;
//document.body.innerHTML = html;
})
</script>
<body>
</body>
</html>
$(document.body).innerHTML = html; // jQuery one is not working either. document.body.innerHTML = html; // normal JS trying to query DOM is not working either.
What's the actually issue here?. I know while using jQuery we don't need to use innerHTML to append the HTML thing, just was trying and learning it out.
Upvotes: -1
Views: 522
Reputation: 707
That self-executing function isn't quite right, you need to change the last line to:
})();
But even once you fix that, because the script is before the opening body tag, it's running before the body has been constructed in the DOM. If you watch the console, you'll see an exception like "Cannot set property 'innerHTML' of null". Moving the script into the body will fix this.
So here's the native JS version:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>css demo</title>
</head>
<body>
<script>
(function(){
var html = '<p>';
html += 'Hello World';
html += '</p>';
document.body.innerHTML = html;
})();
</script>
</body>
</html>
And then here is a jQuery version:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>css demo</title>
</head>
<body>
<script src="jquery.js"></script>
<script>
(function(){
var html = '<p>';
html += 'Hello World';
html += '</p>';
$('body').html(html);
})();
</script>
</body>
</html>
Upvotes: 0
Reputation: 3170
change $(document.body).innerHTML = html;
to $(document.body).html(html);
which is a right way to do it using jQuery.
or
change $(document.body).innerHTML = html;
to $(document.body)[0].innerHTML = html;
if you dont want to use html()
method.
Upvotes: 1
Reputation: 1064
Do like this
<script>
$(document).ready(function(){
var html = '<p>';
html += 'Hello World';
html += '</p>';
$('body').html(html);
})
</script>
Upvotes: 1
Reputation: 8588
You forgot one thing:
(function(){
});
This will not run, you should do:
$(function() {
//...
//and then...
$('body').html(html);
});
When you're using jQuery to select an element, jQuery adds an array wrapper for it so if you want to use both the jquery selector and the native JavaScript html you should do it like:
$(document.body)[0].innerHTML = html;
Upvotes: 1
Reputation: 6926
Since you are using jQuery, you can also do this the jQuery way:
$(function(){
var html = '<p>';
html += 'Hello World';
html += '</p>';
$('body').html(html);
});
Upvotes: 1
Reputation: 75
actually the browser run your script before loaded. you have to setup a timer or add onDomReady event
Upvotes: 1
Reputation: 41605
Use the $.html()
function of jQuery :
This method uses the browser's innerHTML property. Some browsers may not return HTML that exactly replicates the HTML source in an original document. For example, Internet Explorer sometimes leaves off the quotes around attribute values if they contain only alphanumeric characters.
var html = '<p>';
html += 'Hello World';
html += '</p>';
$(document.body).html(html);
Living example: http://jsfiddle.net/YTaRG/
Upvotes: 1
Reputation: 121998
As simple as you can do with html()
is
$('body').html(html);
Upvotes: 2
Reputation: 10378
(function(){
var html = '<p>';
html += 'Hello World';
html += '</p>';
$("body").html(html);
//document.body.innerHTML = html;
})
Upvotes: 1
Reputation: 388316
you need to get the dom element, $(document.body)
returns a jQuery wrapped element which does not have innerHTML
property
$(document.body).get(0).innerHTML = html;
or simple
document.body.innerHTML = html;
using jQuery
$('body').html(html)
also it looks like the anonymous function is not called
(function(){
var html = '<p>';
html += 'Hello World';
html += '</p>';
document.body.innerHTML = html;
})()
if you need to wait for dom ready then
jQuery(function($){
var html = '<p>';
html += 'Hello World';
html += '</p>';
$('body').html(html)
})
Upvotes: 6