1

I'm trying to use Javascript's built-in replace() method, but for some reason it won't allow me to wrap html around a word and output it to a textarea.

$(function() {
    $('#js-convert-markup').click(function() {
        var htmlToConvert = $('.markup_converter').val();
        htmlToConvert.replace('class', '<span class="admin_styles_type">class</span>');
        var convertedMarkup = $('.markup_converted').val(htmlToConvert);
    });
});​

http://jsfiddle.net/someprimetime/fmu5m/11/

Any idea why?

2 Answers 2

2

replace returns a new string, it does not alter the original. You need to do:

htmlToConvert = htmlToConvert.replace(...)
Sign up to request clarification or add additional context in comments.

Comments

1

Try using:

htmlToConvert = htmlToConvert.replace('class', '<span class="admin_styles_type">class</span>');

Strings are immutable in JavaScript.

http://jsfiddle.net/a26nf/

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.