Html string replace a span's class doesn't work in IE?

Posted by SeanJA on Stack Overflow See other posts from Stack Overflow or by SeanJA
Published on 2010-03-15T17:46:54Z Indexed on 2010/03/15 17:49 UTC
Read the original article Hit count: 343

Someone tried to recreate smarty in js:

// actually from a template file, not hardcoded in the javascript
html = '<span class="{test}">yay</span>'; 


html = change(html, 'test', 'foo bar');
function change(html, key, value){
    html = html.replace('{'+key+'}',value);
    html = html.replace('%7B'+key+'%7D',value);
    return html;
}

element.innerHTML = html;

In FF this works fine (as expected): yay

In IE 7/8 and probably 6... it gives me this:

<span class="foo" bar="">yay</span>

Why does it create the extra attribute instead of doing what I expected it to do?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about replace