I need to create a <br />
tag dynamically with javascript.
var br = document.createElement('br');
And
var br = document.createElement('<br />');
doesn't work
The first option urrently creates a <br>
tag on the page which doesn't pass XHTML standards, is there anyway around this?
To create a line break in JavaScript, use “<br>”. With this, we can add more than one line break also.
createElement() In an HTML document, the document. createElement() method creates the HTML element specified by tagName, or an HTMLUnknownElement if tagName isn't recognized.
To add a BR in JavaScript the method createElement() is used. We can use it within our document and append the line break to the DOM like so: const testElement = document. querySelector('div'); const lineBreak = document.
The first method does not need to pass XHTML standards - you're confusing markup with manipulating the DOM.
A Simple Script of HTML and JavaScript to add br tag dynamically in the page.
<SCRIPT language="javascript"> function add() { //Create an input type dynamically. var br = document.createElement("br"); var foo = document.getElementById("fooBar"); foo.appendChild(br); } </SCRIPT> <INPUT type="button" value="Add" onclick="add()"/> <span id="fooBar"> </span> This text will go down every time you click on add button
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