0

In my code, i have an option button collection as this. I want in my javascript file to execute a function when option is changed.

<div class="col-sm-8 user-type" >
  <%= f.collection_radio_buttons :team_account_type, [ ['admin', 'Administrator'], ['team_leader', 'Team Leader'], ['standard_user', 'Standard User']], :first, :last, checked: ['standard_user', 'Standard User']%>
</div>

my js file

 $('#team_account_type').on('change', function() {
        console.log("i have selected team leader")
 });

But it is not working. when i click on the different options, it doesnt catch it or recognize it. Any help appreciated.

Thanks

1
  • What does the generated HTML look like? Does the ID you are matching on get generated as you expect? Commented Sep 28, 2015 at 10:05

1 Answer 1

1

Wrap your code inside $(function(){ ... }); so that change event handler applied on when DOM is ready.

$(function(){
    $("#team_account_type").on("change", function() {
        console.log("i have selected team leader");
    });
});

OR you can delegate the change event using document object

 $(document).on("change","#team_account_type", function() {
            console.log("i have selected team leader");
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.