Jquery strange behavior

Hello.

In my app jquery script runs strange and wrong. So I have usuall .js.erb template, which is called from controller by format.js
This template is just for testing now, so it only calls alert message, if some links are clicked. I didn’t modify default layout document.

The problem is that on first click nothing happens, than it alerts as many times, as all links was clicked until this moment.

So 1st time click - zero alerts, 2 time click - 1 alert, 3 time click - 2 alerts and so on.

The code of Jquery file is so simple, that definetely not the source of problem.

May be some ideas?

But for the sake of stupid errors in simple things I’ll post jquery code:

$(’.keyword_links’).ready(function(){

var keywords = $(’.keyword_links’);

function addField(){

alert(‘True’);

}

keywords.bind(“click”, addField);

});

Barry wrote in post #1101300:

But for the sake of stupid errors in simple things I'll post jquery
code:

$('.keyword_links').ready(function(){
    var keywords = $('.keyword_links');
    function addField(){
        alert('True');
    }
    keywords.bind("click", addField);
});

What you show here is not the proper way to use the ready AFAIK.

Try this instead:

$(document).ready(function() {
  $('.keyword_links').bind('click', addField);
});

function addField() {
  alert('True');
}

// Or the shorthand version
$(function() {
  $('.keyword_links').click(function() { alert('True') });
});

// Even more awesome use CoffeeScript
$ -> $('.keyword_links').click -> alert('True')

Robert Walker wrote in post #1101305:

Barry wrote in post #1101300:

But for the sake of stupid errors in simple things I'll post jquery
code:

$('.keyword_links').ready(function(){
    var keywords = $('.keyword_links');
    function addField(){
        alert('True');
    }
    keywords.bind("click", addField);
});

What you show here is not the proper way to use the ready AFAIK.

Forgot to include the documentation reference:

http://api.jquery.com/ready/

no, this didn’t solve problem

Barry wrote in post #1101311:

no, this didn't solve this ((

It worked as expected in my quick test. You haven't provided enough
information for me to help further. For instance I don't know what your
HTML looks like. How many elements are matched by the jQuery selector,
etc.

ok…

more code.

in tests_controller method which calls js file:

def add_key

@test=Test.find(params[:id])

@key=@test.keys.build

@key.keyword = params[:keyword]

@key.position = params[:position]

@answer = generate_answer_field(@key.position)

respond_to do |format|

if @key.save then

      format.js

end

end

end

it calls add_key.js.erb with code from first post

in show view link on which jquery called:

<%= link_to(result_array[k], {:controller =>‘tests’, :action => ‘add_key’, :keyword => result_array[k], :position=>k}, :class=>‘keyword_links’, :method => :post, :remote => true) + str %>

Forever

And finally to mention, that actually I don’t have real route for this link coz all it need to render some form and save smth to database.

Link works - saves that info to database. Problem goes with Jquery.

A lot of such links from array output. But problem is maintaining with one link etc.

guys, I found out everything. As always, I’m stupid. Hope, this topic will help someone to avoid this type of mistake.

So, I got controller, which respond to js.ERB file.

This js.erb file is called when controller method called, that means it doesn’t suit to handle some events inside of it.

$(’#<%=j @key.position.to_s %>’).hide().after(’<%= j render(‘generate_answer_field’) %>’);

works perfectly) i’m dumb)