ghostCoder
ghostCoder

Reputation: 7655

using JQuery functions on a string of html

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?

Upvotes: 1

Views: 232

Answers (3)

Peeter
Peeter

Reputation: 9382

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

Upvotes: 1

Jess
Jess

Reputation: 8700

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.

Upvotes: 1

Sangeet Menon
Sangeet Menon

Reputation: 9915

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.

Upvotes: 1

Related Questions