1

I have a string of html, on which i want to use jquery functions. The string has divs as below.

var html=        
    <div class = foo>
    some code
    <div class = bar>

    //more divs, code

    </div>
    </div>

I need to select the divs with class 'bar' out of these and use it for something. This is what I'm doing

var jqueryObj = jQuery( html);
jQuery('.bar', jqueryObj).wrap(   //a new div );

but this doesnt seem to be working. its not taking the context of jqueryObj correctly. Any idea where I am going wrong?

1
  • Try jQueryObj.find('.bar') instead. Commented Apr 16, 2011 at 7:56

3 Answers 3

1

You could do something like http://jsfiddle.net/mazzzzz/LMZ4V/

It selects everything with a class of bar, and sets its html to Test, then prints the html to a textarea.

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

Comments

1

if your format of String is always like the above snippet try this..

html.replace("<div class = bar>","<div class=newDiv><div class = bar>");
html+="</div>"

But, this isn't JQuery.

Comments

1
var html= '<div class = foo>some code<div class = bar></div></div>';
var result = $(html);
result.find(".bar").wrap("<div/>")
console.log(result);

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.