Possible Duplicate:
jQuery find and replace string
This is my html:
<div class="dt_lft">There is an error</div>
I need to find whether the div has the text error
and replace that text with success
using class name. How to do this?
Possible Duplicate:
jQuery find and replace string
This is my html:
<div class="dt_lft">There is an error</div>
I need to find whether the div has the text error
and replace that text with success
using class name. How to do this?
You can use the :contains()
selector:
$("div.dt_lft:contains('error')").text(function(i, text) {
return text.replace('error', 'success');
});
If it's likely that the string error
will occur more than once in the text, you'll need to use a RegEx to handle that:
$("div.dt_lft:contains('error')").text(function(i, text) {
return text.replace(new RegExp('error', 'g'), 'success');
});
$("div.dt_lft:contains('error')").text(function(i,text){
return text.replace('error', 'success');
});
$("div.dt_lft:contains('error')")
returns all divs with class dt_lft
containing word error
, you can read more about jQuery contains selector. With jQuery .text()
you can write function like:
$(object).text(function(index,CurrentContent){
//somecode here
}
Elsewhere, if your object contains word error
many times, you can do:
text.split('error').join('success');
In case you do not want to use RegEx.
Try this
var m=$(".dt_lft").html();
m=m.replace("error","success");
$(".dt_lft").html(m);
Try this,
if($("div.dt_lft:contains('error')")){
$('.dt_lft').replace('error','success');
}