Jquery strange behavior

21 views
Skip to first unread message

Barry

unread,
Mar 12, 2013, 6:56:58 PM3/12/13
to rubyonra...@googlegroups.com
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?

Barry

unread,
Mar 12, 2013, 6:59:02 PM3/12/13
to rubyonra...@googlegroups.com
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);
});

Robert Walker

unread,
Mar 12, 2013, 7:50:32 PM3/12/13
to rubyonra...@googlegroups.com
Barry wrote in post #1101300:
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')

--
Posted via http://www.ruby-forum.com/.

Robert Walker

unread,
Mar 12, 2013, 7:54:31 PM3/12/13
to rubyonra...@googlegroups.com
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/
Message has been deleted

Barry

unread,
Mar 12, 2013, 8:14:00 PM3/12/13
to rubyonra...@googlegroups.com
no, this didn't solve problem


среда, 13 марта 2013 г., 2:56:58 UTC+4 пользователь Barry написал:

Robert Walker

unread,
Mar 12, 2013, 9:19:34 PM3/12/13
to rubyonra...@googlegroups.com
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.

Barry

unread,
Mar 12, 2013, 10:03:17 PM3/12/13
to rubyonra...@googlegroups.com
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 %>

<a href="/tests/143/add_key?keyword=Forever&amp;position=18" class="keyword_links" data-method="post" data-remote="true" rel="nofollow">Forever</a>

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.

Barry

unread,
Mar 12, 2013, 10:05:38 PM3/12/13
to rubyonra...@googlegroups.com
A lot of such links from array output. But problem is maintaining with one link etc.

среда, 13 марта 2013 г., 5:19:34 UTC+4 пользователь Ruby-Forum.com User написал:

Barry

unread,
Mar 13, 2013, 12:21:38 AM3/13/13
to rubyonra...@googlegroups.com
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.


Barry

unread,
Mar 13, 2013, 12:49:51 AM3/13/13
to rubyonra...@googlegroups.com
$('#<%=j @key.position.to_s %>').hide().after('<%= j render('generate_answer_field') %>');

works perfectly) i'm dumb)
Reply all
Reply to author
Forward
0 new messages