Skizit
Skizit

Reputation: 44842

Javascript: Replacing text in HTML object

I've got a HTML object obj and I'm attempting to replace a string in it's inner HTML with another..

obj.innerHTML.replace('<a>theoldstring</a><span></span>','thenewstring');

However, the string isn't replacing and is printing the same before and after.. why?

Upvotes: 1

Views: 245

Answers (4)

Muhamad Bhaa Asfour
Muhamad Bhaa Asfour

Reputation: 1035

obj.innerHTML = 'thenewstring';

Upvotes: 0

Berzemus
Berzemus

Reputation: 3668

Your call is just returning the new string. Look in the docs how javascript's replace function works.

obj.innerHTML = obj.innerHTML.replace('<a>theoldstring</a><span></span>','thenewstring');

Upvotes: 3

x2.
x2.

Reputation: 9668

obj.innerHTML = obj.innerHTML.replace('<a>theoldstring</a><span></span>','thenewstring');

Upvotes: 1

Saul
Saul

Reputation: 18041

String.replace() does not modify the original object. Instead it returns a new instance, hence an assigment is needed:

obj.innerHTML = obj.innerHTML.replace('<a>theoldstring</a><span></span>','thenewstring');

Upvotes: 1

Related Questions