0

Is there a shortcut for:

var child = $('<span>error message</span>');
$('#parent').html(child.html());

or, the same

var child = $('<span>error message</span>');
$('#parent').empty().append(child);

EDIT: above example won't work if I try to assign same child to multiple parents, my child object can only belong to a single parent (which is not what I wanted in my case).

I do realize I could do:

$('#parent').html('<span>error message</span>');

but I want to reuse child object.
EDIT: again, this won't work, since child is a DOM object and can only be placed under at most one parent at any time.

I am looking for something like:

var child = $('<span>error message</span>');
$('#parent').content(child);
6
  • 1
    What kind of shortcut? The first example looks as short as it could be to my eyes. Commented Oct 19, 2011 at 14:57
  • If you want to reuse the child object then you'll need to keep a reference to it so no, there is no shorter way than you're already doing Commented Oct 19, 2011 at 14:58
  • both .empty().append(...) and .html(child.html()) are too verbose (exhibit unnecessary details superfluous to my intentions). Plus .html(child.html()) feels suboptimal (reconstruct-deconstruct HTML) Commented Oct 19, 2011 at 14:59
  • @Felix Kling: what I am trying to do is precisely this: 'set content of an element'. Neither of my examples concisely state that (too much details). If there is not built-in jQuery method for that, I will use empty().append(). Commented Oct 19, 2011 at 15:03
  • using html(content) on an element will simply overwrite anything that was in there in the first place, no need to use empty Commented Oct 19, 2011 at 15:06

4 Answers 4

2

You can always wrap functionality in a plugin:

(function($) {
    $.fn.setContent = function(content) {
        return this.empty().append(content);
    };
}(jQuery));

$('#parent').setContent(child);

Also if you want to reuse an existing jQuery object, you probably have to use .clone() if you want to insert it later somewhere else.

Sign up to request clarification or add additional context in comments.

6 Comments

you missed $.each() on this. other than that this would pretty-up my code, but the original question was is there a shortcut not a how can I pretty this up. Valid solution though. +1
@user93422: Why should I need each?
"I need to make this code shorter" "you could always write a new plugin!" hmmm. Would that give you a different result than $('#parent').html(child);?
@Tules: Actually not... but as I was saying, passing a DOM element is not described in the documentation so we don't know whether this is an intended feature or not. One could also argue that setContent is more expressive than html.
if content is a jquery object (in my example it is a DOM element) thin it is not possible to assign it to multiple parents. I.e. my original examples are correct but invalid. I have to copy my object by using .html() after all.
|
1
var child = '<span>error message</span>';
$('#parent').html(child);

is the shortest you are possibly gonna get, how short does it really need to be? lol

5 Comments

I would prefer to have child pre-built, but a valid solution nevertheless. +1
in that case ur first example really is the shortest ur gonna get
see my edits. child as a DOM object can not be reused, for that reason your answer is the correct one. I would replace child = '...' with child = $(...).html() for the convenience of being able to specify attributes using .attr().
...and I'm wrong again. .html() will give me content of the child DOM element, not an 'outerHtml'. So your solution is pretty much as good as I can get without plugins/extensions.
using .clone() on the child would allow to reuse a child pre-built as a DOM element.
0
var child = $('<span>error message</span>').attr({ 'class' : 'error-message'});
$('#parent').html(child.clone()); 

notice that you have to clone() child unless you want to actually move child from its previous parent.

credits to both Tules and Felix Kling

Comments

-1

you could try something like

      var child = $('#parent').html('<span>error message</span>').children('span:eq(0)');

Ive not tried it myself. To be honest your first example seems pretty good to me.

1 Comment

Right... and I'm just saying that your code is not correct. Not trying is not an excuse for posting erroneous code imo, especially when it contains syntax errors.

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.