1

in my index.html.erb I have:

<button type="button" id="clean">Clean</button>

This button is add after by action "something.js.erb". So, in application.js:

$(function () {
    $('button#clean').click(function(){
        $('#target').remove(); 
    });
});

why is not working? When I put this script into html, it's work.

5 Answers 5

2

Use "on function" in a correct format.

$(document).on('click', 'button#clean', function () {
    $('#target').remove();
});
Sign up to request clarification or add additional context in comments.

Comments

0

Are you referencing your .js file in your HTML ??

Also Hit F12 to check for any errors in the console section

If you see no errors then try to attach the event handler using .on()

$('button#clean').on('click', function() {
});

2 Comments

Yes,such: <script src="/assets/application.js?body=1" type="text/javascript"/>
Try placing a alert in DOm ready event and check if jQuery was loaded properly in the first place
0

Make sure your loading jQuery beforehand and I suggest using the following code

 $('button#clean').on("click", function() {
 });

Comments

0

I'd check your console to ensure there are no errors and ensure you've included application.js and jQuery within your HTML.

index.html.erb

<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/application.js"></script>

application.js

(function ($) {
  $('button#clean').click(function () {
    $('#target').remove();
  });
})(jQuery);

1 Comment

Don't appears nothing in console.
0

If you want page specific js to be executed before going to the index.html for foo_controller then do

assets/javascripts/foo/index.js

and place

$(function () {
    $('button#clean').click(function(){
        $('#target').remove(); 
    });
});

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.