0

I am using the below function to create a div dynamically which works fine so far.

How can I add HTML to this div where I am saying "text: userName" ? Specifically I would like to add a Bootstrap icon ( <i class="icon-sign-blank"></i> ) in front of the userName but when I try this is just adds the HTML as text instead of formatting it as an icon. (Bootstrap is installed correctly.)

function addUser() 
{
var userName = $('#userName').val();
var userElementId = getId(userName);

$('<div/>', {
    text: userName,
    id: userElementId,
    onmouseover: "function1('" + userName + "', this)",
    onmouseout: "function2()",
}).addClass('user').appendTo('#parentDiv');

if (nameCtrl) {
    nameCtrl.GetStatus(userName, 'users');
}

$('#userName').val('');
}

Many thanks for any help with this, Tim

2
  • 1
    html: userName+"<i class="icon-sign-blank"></i>", ??? Commented Dec 12, 2013 at 12:42
  • Thanks, that worked. I had tried it before but probably had a typo there. Commented Dec 12, 2013 at 12:47

3 Answers 3

1

Change text: to html: and add the icon before username.

$('<div/>', {
    html: '<i class="icon-sign-blank"></i> '+userName,
    id: userElementId,
    onmouseover: "function1('" + userName + "', this)",
    onmouseout: "function2()",
}).addClass('user').appendTo('#parentDiv');
Sign up to request clarification or add additional context in comments.

Comments

1

You are setting the text: attribte. Try setting the html: attribute with the html you want.

Comments

0

try something like this

var div = '<div><i class="icon-sign-blank"></i>'+userName+'</div>';
$(div, {
    id: userElementId,
    onmouseover: "function1('" + userName + "', this)",
    onmouseout: "function2()",
}).addClass('user').appendTo('#parentDiv');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.