1

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?

4 Answers 4

3

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');
Sign up to request clarification or add additional context in comments.

Comments

1

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');

Comments

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

Comments

0
obj.innerHTML = 'thenewstring';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.