0

I'm currently using jquery .html for DOM insertion. Is there a way I can insert multiple elements?

So far I have tried using && or even using a comma inserting multiple elements but none of them seems to be feasible. Separating it would also likely not work.

$('#header .aim-breadcrumbs').html($('.layout-container .region-breadcrumb') && $('#header .aim-nav .connect')); $('#header .aim-breadcrumbs').html($('.layout-container .region-breadcrumb', '#header .aim-nav .connect'); 

$('#header .aim-breadcrumbs').html($('.layout-container .region-breadcrumb'); $('#header .aim-breadcrumbs').html($('#header .aim-nav .connect'); 
5
  • 1
    I think you want .append() not .html(), and you need to specify elements, not just a list of classes and/or IDs Commented Jun 15, 2017 at 2:33
  • 1
    .html() will override the previous one if you want multiple then this is not the correct to use. try .append() Commented Jun 15, 2017 at 2:34
  • 1
    use .append() → check this link: api.jquery.com/append Commented Jun 15, 2017 at 2:35
  • 1
    If you want to use the same div for multiple things .html would be better because you don't need to clear the contents before appending. Commented Jun 15, 2017 at 2:38
  • Are you trying to move existing elements around, or insert/add new elements to the page? Commented Jun 15, 2017 at 2:44

1 Answer 1

3

You are really close. You just need to put the comma inside your selector string. Don't use two strings.

$('#header .aim-breadcrumbs').html($('.layout-container .region-breadcrumb, #header .aim-nav .connect')); 

This will work, but as everyone is already shouting, .append is the typical way of doing this.

$('#header .aim-breadcrumbs').append($('.layout-container .region-breadcrumb, #header .aim-nav .connect')); 
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what kind of magical unicorn elements will this add to the document? You can't just list classes and IDs and no actual elements and try to append them to something. You're also missing closing parenthesis.
I fixed the parenthesis. It's not just adding random classes. If you pass a jquery object like $(.selector), it will move or copy that element for you, depending on the method you use.
Thank you for that insights, wasn't aware that inserting a comma inside the string would be valid

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.