0

I am trying to dynamically create objects using JQuery. I have a json object with elements I want to pull and add onto the object. So far I've been using this line and I am getting text on my object:

$(this.node).find('a').text(dict.get('text1'));

I want to add a second object, "text2", with a paragraph tag. What is the function to add tags to a jquery object in a similar manner to how I've been adding text?

Edit: I want to end up with an object that has two paragraph tags which I can add class/id tags to

4
  • .append() is what you're looking for. Commented Aug 16, 2013 at 12:26
  • Isn't data what you're looking for: api.jquery.com/data Commented Aug 16, 2013 at 12:27
  • For append, the problem is that old text is retained when new text is added. Is there anyway around that? Commented Aug 16, 2013 at 12:31
  • What is your exact need? can you just explain with sample Commented Aug 16, 2013 at 12:32

2 Answers 2

3

You need .append() function:

Description: Insert content, specified by the parameter, to the end of each element in the set of matched elements.

documentation

If you want to add a paragraph, you can do this:

var $p = $("p");
$p.text("This will be the paragraph text.");
$(".container").append($p);

Also here you find the insertion-inside functions.

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

1 Comment

For append, the problem is that old text is retained when new text is added. Is there anyway around that?
1

You have a few options depending on how/where you want to insert your tags relative to existing content: append() or appendTo(), prepend(), after() or insertAfter(), before() or insertBefore(). If you are inserting multiple tags in the same area, you also can 'concatenate' them first using these functions, before actually inserting it to your larger jQuery object.

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.