44

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?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Phil
  • 10,948
  • 17
  • 69
  • 101

6 Answers6

15

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">&nbsp;</span>

    This text will go down every time you click on add button
Nagri
  • 3,008
  • 5
  • 34
  • 63
15

The first method does not need to pass XHTML standards - you're confusing markup with manipulating the DOM.

jball
  • 24,791
  • 9
  • 70
  • 92
  • The way it's expressed makes it can be construed that XHTML standards matter, just not in the first method. – RobG Oct 06 '11 at 00:04
9

The first option will work and has nothing to do with XHTML since the DOM operations are performed after parsing of the document, and therefore there is no XHTML/HTML standard to be compliant to at that point. As long as you are not trying to output the HTML to a string, this approach will work just fine.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
4

This question has nothing whatever to do with markup. The argument passed to createElement is an element name, it is not a fragment of markup or anything else, it isn't XML, or HTML, or XHTML, or SGML or any form of markup.

The createElement method returns a DOM object, there is no markup and never was.

Note that the markup passed to the browser is used to create a document object, from that point onward, the markup is irrelevant.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • 1
    That the question has nothing to do with markup (which all of the answers were fixated on), it is about the element name provided to the function. Others will stuble across this question like I did, hopefully they'll see all the answers and get a better idea of the actual issue. – RobG Oct 06 '11 at 00:01
3

In reality, your code will still validate if your JavaScript doesn't exactly validate, because the validator only parses the processed HTML code, not the dynamic code created by JavaScript. If you cared, you could also use an innerHTML trick, or a document.createTextNode('<br />'); if you wanted to force the XHTML version, but it is not recommended. document.createElement('br'); works perfectly fine.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
2

Unless "<br>" is passed as innerHTML to any element, it will not be rendered as an HTML tag. So we can use a dummy '<span>' element and keep '<br>' as its innerHTML so that it will be rendered as expected.

br = document.createElement("span");
br.innerHTML = "<br/>";
document.body.appendChild(br);