Parag Srivastava
Parag Srivastava

Reputation: 235

Adding a tag to a JQuery object

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

Upvotes: 0

Views: 95

Answers (2)

Ionică Bizău
Ionică Bizău

Reputation: 113365

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.

Upvotes: 3

Frank Fajardo
Frank Fajardo

Reputation: 7359

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.

Upvotes: 1

Related Questions