Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:56:58+00:00 2026-06-15T01:56:58+00:00

So I’m building a little set of Markdown helpers for an ActiveAdmin Form. I

  • 0

So I’m building a little set of Markdown helpers for an ActiveAdmin Form. I just have the italics one so far.

#form.html.erb
# ... some form code
    <%= link_to "i", "#", :class => "btn", :id => "italics_button" %>
    <%= f.input :body %>
# ... rest of form omitted

Which gives me the button with an ID of “italics_button” and a textarea with an ID of post_body. So far, all is well.

Now I have a coffeescript file to handle the wrapping the selected text in a given tag.

#posts.js.coffee
wrapText = (elementID, openTag, closeTag) ->
  textArea = $("##{elementID}") #select the text area
  len = textArea.val().length #total length of the text area
  start = textArea[0].selectionStart # start of the selected text
  end = textArea[0].selectionEnd # end of the selected text
  selectedText = textArea.val().substring(start, end) # The selected Text
  replacement = openTag + selectedText + closeTag # string with the selected text wrapped in the bbcode
  textArea.val(textArea.val().substring(0,start) + replacement + textArea.val().substring(end, len)) # perform the replacement

$('#italics_button').click (event) ->
  event.preventDefault()
  wrapText('post_body', '*', '*')

I’m fairly confident this code is okay, because I ripped it out of a project from a couple months ago where I did the same thing on a normal non-AA form.

I’ve updated the initializer to bring in the custom javascript:

# active_admin.rb
# rest of file omitted
config.register_javascript 'posts.js.coffee'

And finally, I can see on the New Post page in Active Admin, the javascript file is included and compiled.

However, the javascript event does not appear to be called. When I click #italics_button, the page attempts to follow the link to “#” and the javascript does not run.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:56:59+00:00Added an answer on June 15, 2026 at 1:56 am

    This will be executed as soon as the browser sees it:

    $('#italics_button').click (event) ->
      event.preventDefault()
      wrapText('post_body', '*', '*')
    

    Your CoffeeScript will probably be loaded in the page’s <head> element so there probably won’t be any #italics_button in the DOM when you $('#italics_button').click(...) so no callback will be bound.

    You can try the usual “run when the document is ready approach”:

    $ ->
      $('#italics_button').click (event) ->
        event.preventDefault()
        wrapText('post_body', '*', '*')
    

    or you could use a “live” handler:

    $(document).on('click', '#italics_button', (event) ->
        event.preventDefault()
        wrapText('post_body', '*', '*')
    )
    

    The latter will be useful if the #italics_button will be added dynamically after the server has sent the page to the browser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.