Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I add a <br /> with JQuery .html?

Tags:

html

jquery

dom

Why does this code work:

$('div.error_container').html('<div class="error">No more foo allowed</div>');

But this code causes an error:

$('div.error_container').html('<div class="error">No more foo allowed<br /></div>');

I've tried putting the <br /> before and after the </div> tag, same error. I've tried changing it from <br /> to <br>. Chrome always says the following in the inspector:

Uncaught SyntaxError: Unexpected token ILLEGAL
like image 500
Paul Tomblin Avatar asked Jan 31 '10 22:01

Paul Tomblin


People also ask

How can add break tag in jQuery?

You can use . after() to insert an element after each the selector matched, like this: $("#twitter_update_list span"). after("<br />");

What are jQuery objects?

A jQuery object is created from the array elements in the order they appeared in the array; unlike most other multi-element jQuery operations, the elements are not sorted in DOM order. Elements will be copied from the array as-is and won't be unwrapped if they're already jQuery collections.

What is add in jQuery?

The add() method adds elements to an existing group of elements. This method adds elements on the whole document, or just inside context elements if the context parameter is specified.


1 Answers

Try breaking it up into a chain:

var $div = $('<div class="error"></div>').html('No more foo allowed')
                                         .append('<br />');
$('div.error_container').html($div);
like image 179
karim79 Avatar answered Sep 22 '22 01:09

karim79