I need to be able to create a function in JavaScript where all I need to do is type h1("hello") and it will print hello.
I want to avoid this method:
function h1(text) {
document.write('<h1>'+text+'</h1>');
}
This is what I have:
function h1(text) {
var div = document.createElement('div');
document.appendChild(div);
var h1 = document.createElement('h1');
div.appendChild(h1);
h1.createTextNode(text);
}
Approach 1: Use document. createElement() to create the new elements and use setAttribute() method to set the attributes of elements. Append these elements to the <form> element by appendChild() method. Finally append the <form> element to the <body> element of the document.
Browser support: Specifies document heading. There are predefined heading elements that allow document styling in HTML pages similar to Microsoft Office Word or other word processing software, H1 is the main heading and H6 is the deepest sub-heading element.
Your H1 Should Describe the Topic of Your Page Often, the H1 tag will be similar or the same as your title tag. Usually, the H1 tag will be the title of your blog post or article.
<script>
function myFunction(text) {
var h = document.createElement("H1");
var t = document.createTextNode(text);
h.appendChild(t);
document.body.appendChild(h);
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With