1

I am using createElement to replace it with appending big chunk of html code. I understand I can add id, class, style easily with:

var container = 'container';

container.className = 'item';
container.style.cssText = 'color: red';

However, how can I add sub-id using this approach?

<div class="container">
    <i class="item" sub-id="item-19"></i>
</div>
2
  • 1
    why you tagged jquery while you just use pure javascript? anyway in jquery just use $('i.item').attr('sub-id','item-19'); Commented Oct 25, 2015 at 3:36
  • 1
    Try container.setAttribute("sub-id", "item-19"). Documentation is here and many other places. Commented Oct 25, 2015 at 3:37

1 Answer 1

2

If you are using jQuery then I believe you are looking for something like this:

var container = $('<div />').attr('class','container');
var subId = $('<i />').attr({ 'class':'item', 'sub-id':'item-19'}).text('italic');

//append it to the body

$('body').append(container.append(subId));

Using the createElement methods would be more time consuming to program (but should be faster than jQuery). As for using the sub id in your createElement methods take a look at the API for an Element Object

container.setAttribute("sub-id", "item-19");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.