More information about the Underscore mailing list

[_] JQuery question - code works in console, not in page

Nick Morgan skilldrick at gmail.com
Fri Sep 9 11:23:23 BST 2011

On 9 September 2011 11:20, Oliver Humpage <oliver at watershed.co.uk> wrote:
>
> On 9 Sep 2011, at 11:07, Keir Moffatt wrote:
>
>> $('#test2').html().replace(/"$/,'“');
>
> That line will generate a string that has the quote replaced with the HTML
> entity as you want. However, you don't then do anything with that string,
> like render it back to the document. replace() acts on strings, it's not an
> in-place perl -pi type thing.
>
> So you want to actually say "Set the HTML of #test to (the HTML of #test
> with a bit replaced)"
>
> or
>
> $('#test').html($('#test').html().replace(/"$/,'“'));
>
> which I think is your test3 on your fiddle.
>

Yup. The only problem there is if you've got any event handlers and
you do a full .html() replacement, you'll lose them. That's where the
jQuery  replaceText plugin comes in handy.

-- 
Nick Morgan
http://skilldrick.co.uk
@skilldrick

Save our in-boxes! http://emailcharter.org