Novellizator
Novellizator

Reputation: 14883

How to operate on custom HTML with jQuery?

let's say I have

str = "hello, <b>my name is</b> jQuery.";

And I want to use $ selector, to get the "my name is". What should I do? So far I tried this:

html = $.parseHTML( str );
$( $(html), "b")

Without much success...

It only returns an array of all elements, but not the chosen element...

EDIT: I cannot modify str.

Upvotes: 0

Views: 56

Answers (3)

user4655581
user4655581

Reputation:

Try giving the <b> tag an id and then get it by using jQuery.

str = "hello, <b  id="id">my name is</b> jQuery.";

$("#id").val();

Hope this helps.

Upvotes: 0

Girish
Girish

Reputation: 12127

In jQuery selector find element always add first, try this code

str = "hello, <b>my name is</b> jQuery.";
html = $.parseHTML( str );
$('b' ,$(html))

OR you can create Empty dom element and put content inside element after that you can operate that DOM as per your requirement

var str = "hello, <b>my name is</b> jQuery.";
$('<div/>', {html: str}).find('b').html();
//or other short variant
$('>b', $('<div/>', {html: str})).html()

Upvotes: 1

Legendary
Legendary

Reputation: 2242

Try to solve with regexp

var str = "hello, <b class='needsThing'>my name is</b> jQuery.";
var reg = new RegExp(/<b>.*<\/b>/)
result = str.match(reg)

Is it, what u want?

Upvotes: 0

Related Questions