8

Is is possible to select specific text string on the page directly without a id, class, etc...

I have this text string "ERROR: AffiliateID invalid" that I would like to remove from the page.

Is it possible?

user357034
  • 10,731
  • 19
  • 58
  • 72
  • http://stackoverflow.com/questions/1175775/find-all-instances-of-old-in-a-webpage-and-replace-each-with-new-using-a-jav - this may help you – MartyIX Aug 16 '10 at 22:02
  • Do you have *any* idea as to which element contains the text? Even just the nested position of the element on the page? Something so that the text can be targeted directly? – user113716 Aug 16 '10 at 22:12
  • Funny because it appears before the tag when looking at the source in FF------> ERROR: AffiliateID invalid – user357034 Aug 16 '10 at 22:19

1 Answers1

12

This should solve the problem imho:

document.body.innerHTML = document.body.innerHTML.replace( /ERROR: AffiliateID invalid/g, "");

The code replaces all occurences of the string with empty string.

EDIT: Use it only if you're 100 percent positive that it won't break your HTML in any way.

EDIT 2:

I don't know about a specific jQuery function for this. I found this tip:

1) http://johannburkard.de/blog/programming/javascript/6-quick-jquery-tips-text-manipulation-timers-and-elements.html

Remove a word with jQuery

The simple way – using regular expressions:

var el = $('#id'); el.html(el.html().replace(/word/ig, ""));

The example uses the same replace function as the version in pure javascript. The function comes from JavaScript 1.2 (the standard that is 10 years old and is implemented in all major browsers). So I guess there's no reason to use a function from jQuery.

2) There is a plugin for replacing text in specified HTML tags. Maybe it is worth trying.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • This works perfect. I take it it is a javascript version. Jquery version of the same? Also is there a way to put that text in an alert box and then remove it? – user357034 Aug 16 '10 at 22:14