0

I have the following vars:

   $delBtn = $('text');
   $updateBtn = $('text2');

Now, if I want to insert one inside another element, I would use:

  $element.html($delBtn)

But how can I inset them as a concatenated pair?

 $element.html($delBtn + $updateBtn)

Does not appear to work?

1
  • You can use append $element.append($updateBtn). Commented Oct 14, 2014 at 13:40

2 Answers 2

5

Use .append(), which'll take as many parameters as you need:

$element.append($delBtn, $updateBtn);

If you need to empty it first, that's easy to do:

$element.empty().append($delBtn, $updateBtn);
Sign up to request clarification or add additional context in comments.

Comments

3

You can either concatenate the HTML as strings:

 $element.html($delBtn.html() + $updateBtn.html())

Note: This will copy the elements and not move them.

Or add the elements into a single jQuery object/collection to append:

 $element.empty().append($delBtn.add($updateBtn));

or http://jsfiddle.net/TrueBlueAussie/0bp2h0Lu/

 $element.empty().html($delBtn.add($updateBtn));

Note: These will move the elements (which is what you specified).

You are better off using the other methods like empty() to clear it and append() to add children as that will be faster than re-parsing the HTML and re-creating elements.

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.