# Jquery strange behavior

**URL:** <https://www.ruby-forum.com/t/jquery-strange-behavior/227361>\
**Category:** Rails\
**Created:** [March 12, 2013, 11:57pm UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361 "2013-03-12T23:57:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 12, 2013, 11:57pm UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/1 "2013-03-12T23:57:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 12:00am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/2 "2013-03-13T00:00:18Z")

</div>

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);  
});

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 12:50am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/3 "2013-03-13T00:50:32Z")

</div>

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’)

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 12:54am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/4 "2013-03-13T00:54:31Z")

</div>

Robert W. 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:

> **[.ready() | jQuery API Documentation](https://api.jquery.com/ready/)**

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 1:15am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/5 "2013-03-13T01:15:13Z")

</div>

no, this didn’t solve problem

, 13 2013 ., 2:56:58 UTC+4 Barry :

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 1:15am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/6 "2013-03-13T01:15:13Z")

</div>

no, this didn’t solve this ((

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 2:19am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/7 "2013-03-13T02:19:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 3:06am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/8 "2013-03-13T03:06:55Z")

</div>

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](http://Ruby-Forum.com) User  
:

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 3:04am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/9 "2013-03-13T03:04:34Z")

</div>

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](https://www.ruby-forum.com/tests/143/add_key?keyword=Forever&position=18)

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.

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 5:51am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/10 "2013-03-13T05:51:06Z")

</div>

$(’#\<%=j @key.position.to\_s %\>’).hide().after(’\<%= j  
render(‘generate\_answer\_field’) %\>’);

works perfectly) i’m dumb)

---

<div class="post-metadata">

**Author:** ![barry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@barry](https://www.ruby-forum.com/u/barry)\
**Post date:** [March 13, 2013, 5:23am UTC](https://www.ruby-forum.com/t/jquery-strange-behavior/227361/11 "2013-03-13T05:23:03Z")

</div>

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.
