Because you've retrieved the text, and then used it as markup:
$("#add").click(function() {
var userList = $('#textarea').val(); // Retrieves the text
$('#textarea').val('');
var newitem ='<p>'+ '*' +userList+'</p>'; // Uses it as markup
$('#list').append(newitem); // on these two lines
});
If you don't want to use it as markup, use jquery's text
function:
$("#add").click(function() {
var userList = $('#textarea').val();
$('#textarea').val('');
var newitem = $('<p>').text('*' +userList);
$('#list').append(newitem);
});
There, instead of using string concatenation to create the p
element, I create the p
element ($('<p>')
), then append the text to it as text, and then append that to the list.
Alternately, you can just encode <
and &
as <
and &
:
$("#add").click(function() {
var userList = $('#textarea').val(); // Retrieves the text
userList = userList.replace(/&/g, "&").replace(/</g, "<"); // Encodes & and < as HTML entities
$('#textarea').val('');
var newitem ='<p>'+ '*' +userList+'</p>'; // Uses it as markup
$('#list').append(newitem); // on these two lines
});
...but I'd use text
.