1

My jQuery question I beleive is pretty simple, which is driving me insane that I can't get it.

I have an object with a property "content", I want to be able to take that object, manipulate the property "content" with jQuery and then overwrite the value with the new value jQuery creates.

Example:

o.content = "<div><span>hello</span></div>";
$('div', o.content).addClass('test');

At this point I want o.content to be equal to <div class='test'><span>hello</span></div>

I can not for the life of me figure out the syntax. Any help is really appreciated.

2
  • Hard to tell what you want to do, please rephrase. What do you think the second parameter to $('div', param) does? See api.jquery.com/jQuery Commented Nov 16, 2012 at 20:48
  • 1
    So you want an HTML string or a jQuery object? Commented Nov 16, 2012 at 20:48

7 Answers 7

2

This will give you a string <div class="test"><span>hello</span></div> if this is what you want:

$(o.content).addClass('test').wrap('<div>').parent().html();
Sign up to request clarification or add additional context in comments.

Comments

2

Parse the html in o.content, add the class, append the parsed html to a new <div>, and get the html of the new div:

o.content = "<div><span>hello</span></div>";
var el = $(o.content).addClass('test');
o.content = $("<div>").append(el).html();

Edit: This assumes you want o.content to still contain a string, rather than a jQuery object. In that case, it's simpler:

o.content = $(o.content).addClass('test');

3 Comments

This could possibly be what the OP wanted, do you have a crystal ball?
@JuanMendes - He said he wants o.content to change from "<div><span>hello</span></div>" to "<div class='test'><span>hello</span></div>". This does that. That is, assuming he wants a string and not a jQuery object. If Musa's comment is addressed, we'll find out.
I was just marveling at how you figured that out from the question (now that it's been rephrased, it's easier to see it)
1

from the docs of the jquery function, context must be

A DOM Element, Document, or jQuery to use as context

Your context (o.content) is a string. Also, the jQuery function is not able to select the entire context, it can only select elements in that context.

Try this instead:

// make o.content a jquery element, not a string
o.content = $("<div><span>hello</span></div>");

// select on something inside the context (inside the div), not the div itself
$('span', o.content).addClass('test');

http://jsfiddle.net/JfW4Q/

Comments

1

I don't think you can lookup an element from a string like that.. I would rather do it like below,

var content = "<span>hello</span>";
content = $('<div/>', {class: 'test'}).html(content)

DEMO: http://jsfiddle.net/k4e5z/

Comments

1

You want the following

o.content = "<div><span>hello</span></div>";
// Create a jQuery object you can call addClass on
var docFragment = $(o.content);
docFragment.addClass('test');
// Since there's no outerHTML in jQuery, append it to another node
var wrapper = $('div');
docFragment.appendTo(wrapper);
// The HTML of the wrapper is the outerHTML of docFragment
console.log(wrapper.html()); // outputs <div class='test'><span>hello</span></div>

Comments

1

Why not do it all in one line:

var o = {};
o.content = $( "<div></div>" )     // create element
    .addClass('test')              // add class
    .html( '<span>hello</span>' ); // append content

Fiddle: http://jsfiddle.net/kboucher/eQmar/

Comments

1
o.content = $("<div><span>hello</span></div>");
o.content.addClass('test');

o.content is a jQuery object in this example, as opposed to just a string. Here's a demo on jsfiddle: http://jsfiddle.net/cvbsm/1/

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.