Skip to main content

Saved messages as HTML

General • Asked by Martin McDermid

Hi Chris,

I'm wondering, on your episode https://gorails.com/episodes/message-templates, would it be possible to have one of the saved messages as HTML? I've tried entering <h1>test</h1><p>this is a test</p> but nothing appears once selected (just some whitespace). Plain text works fine.

This is what I see in the source:

        <option data-body="&lt;h1&gt;test&lt;/h1&gt;&lt;br&gt;&lt;p&gt;this is a test&lt;/p&gt;" value="1">Test</option>

It's obviously formatting it, but am I able to disable this?

Thanks Chris, I appreciate it.


Hey Martin!

This is actually a small bug you find in the JS for that episode. The append() in handleChange works fine for adding plain strings, but not HTML ones to the value of the inputs. If you change the append line to the following, you'll see that it works just fine:

    comment_body.val comment_body.val() + saved_message_text

This is a better way of adding text to a field, where you set the value to the old value plus your new text. Append seems to do that, but not when there is html that you want to add for some reason.

I updated the repository to fix that bug as well. 👍


That's brilliant Chris, works perfectly! :) Thanks very much. Been playing around with it all night but I'm quite the novice when it comes to JS!


Yeah, that was one of those weird things that jQuery does. It'll work as you might think, but not completely. If you were writing this with plain JS, you'd use the same code as I did in my solution basically. No funny business that way.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.