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 8920129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:11:27+00:00 2026-06-15T06:11:27+00:00

I am writing a form in Ruby on Rails and want to have a

  • 0

I am writing a form in Ruby on Rails and want to have a select box that calls a Javascript function. In the form file, this is the line I am using to try to add the select box:

<%= f.select :question_select, Question.all, :prompt => "New Question", :onchange => "displayQuestion(this)" %>

In my application.js file, I just have:

function displayQuestion(link) {
    alert("Changed question");
}

I am going to be dynamically adding these form elements to a page, so I can’t just use jQuery in the application.js file to add a function to a specific form element. Can anyone tell me what I’m doing wrong?

  • 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-15T06:11:28+00:00Added an answer on June 15, 2026 at 6:11 am

    As you may know, Rails 3 strongly encourages UJS (unobtrusive JavaScript), which basically means that the JavaScript does the heavy lifting, and that you shouldn’t tie your client-side interactivity in with your form generators. I would recommend doing something very similar here–just because you’re adding elements dynamically doesn’t mean you can’t use jQuery to watch for changes on them.

    In your template:

    <%= f.select :question_select, Question.all, {prompt: "New Question"}, data: { question: true } %>
    

    This creates something like the following:

    <select id="..." data-question="true">
      ...
    </select>
    

    Then, in JavaScript, you can use event delegation to watch for change events on any element with data-question set on the entire document:

    $(function() {
      $(document).on('change', '[data-question]', function() {
        // this == the element that fired the change event
        alert('Changed question');
      });
    });
    

    Note: Instead of using data-question, you could simply add a class to the element, and then modify your jQuery to use that class:

    $(function() {
      $(document).on('change', '.question', function() {
        // this == the element that fired the change event
        alert('Changed question');
      });
    });
    

    I generally try to minimize the use of CSS selectors in my JavaScript so that designers are free to change them to whatever they want without breaking things, but it works just as well.

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

Sidebar

Related Questions

I'm writing a search form for my Rails (2.3.9 but I've checked that this
I'm writing some form. I have one field in this form that contains a
I am writing an imageboard in Ruby on Rails. I have a form for
I'm writing a web form for my Ruby on Rails app. The form has
I am writing ruby on rails application, which will have 2 different users types
I am writing an Ajax request form with Ruby on Rails using a collection_select
so I'm writing this form for teams and doing this 'add player' function where
Im writing a form to post data off to paypal, and this works fine,
I'm writing a form in XAML that has multiple buttons with content of different
I have a strange problem with the validation I am writing on a form.

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.