tags:

views:

39

answers:

2

Hi, I have a very complex DOM structure, A small for example or say demo of code,

<div>
<div class="test_data">
    <span>sample 1</span>
    <span>sample 1</span>
    <span>sample 3</span>
    <span>sample 1</span>
    <span>sample 2</span>
</div>
</div> 

Now on above example, I have to find all the tag having sample 1 and want to replace it by another string say as example, I want to do this stuff using jQuery.

+2  A: 
$("span:contains('sample 1')").text("example");

Or if you need all spans in a specific div, add it before the span:

$("#somedivId span:contains('sample 1')").text("example");

easy peasy :)

Marko
$("span:contains('sample 1')").text("example");
Bang Dao
my cat ate the quote :/
Marko
Thanks, Nicely explained:)
saorabh
+1  A: 
$('.test_data span:contains(sample 1)').html('sample');

crazy demo

but I think you want more than this. Well maybe this will help someone.

$('.test_data span:contains(sample 1)').html(function(i,html){
     return html.replace('sample 1','sample');
});

this will replace the particular text.

crazy demo

Reigel
Thanks Reigel :)
saorabh