# Client-side javascript question

**URL:** <https://www.ruby-forum.com/t/client-side-javascript-question/172660>\
**Category:** Rails\
**Created:** [July 24, 2009, 8:44pm UTC](https://www.ruby-forum.com/t/client-side-javascript-question/172660 "2009-07-24T20:44:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![muckypup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/43a26b/32.png) [@muckypup](https://www.ruby-forum.com/u/muckypup)\
**Post date:** [July 24, 2009, 8:44pm UTC](https://www.ruby-forum.com/t/client-side-javascript-question/172660/1 "2009-07-24T20:44:43Z")

</div>

Hey

I’m new to Rails, and I was wondering if there was a way to include  
custom javascript libraries in the HTML views? I’ve been searching for  
a while and can’t seem to find any way to do this.

For example, I want to have some javascript function firing an onkeyup  
event from an input element that is generated dynamically. It’s  
nothing to do with ajax – all client side. Everything I’ve found just  
deals with remote calls. I suspect it is something to do with  
observe\_field :function, but can’t seem to get it working.

Thanks.

---

<div class="post-metadata">

**Author:** ![muckypup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/43a26b/32.png) [@muckypup](https://www.ruby-forum.com/u/muckypup)\
**Post date:** [July 24, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/client-side-javascript-question/172660/2 "2009-07-24T20:52:19Z")

</div>

On Jul 24, 7:30 pm, muckypup [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> observe\_field :function, but can’t seem to get it working.

Just go ahead and write the javascript as you normally would. the  
helpers can be handy in some cases but don’t be afraid of ignoring  
them

Fred

---

<div class="post-metadata">

**Author:** ![muckypup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/43a26b/32.png) [@muckypup](https://www.ruby-forum.com/u/muckypup)\
**Post date:** [July 24, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/client-side-javascript-question/172660/3 "2009-07-24T21:05:47Z")

</div>

Ah. At first I thought I had done that, but I must have mistyped it  
when I first did it. I wish I had asked earlier now, just spent quite  
a while banging my head against a wall for nothing. ☹

Stupid :onkeyup =\>.

Thanks. 😃

On Jul 24, 7:50 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![muckypup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/43a26b/32.png) [@muckypup](https://www.ruby-forum.com/u/muckypup)\
**Post date:** [July 25, 2009, 7:03pm UTC](https://www.ruby-forum.com/t/client-side-javascript-question/172660/4 "2009-07-25T19:03:54Z")

</div>

I like the javascript\_include\_tag helper to keep the javascript out of  
the view.

In one of my views I have a collection of radio buttons with class  
“our\_aircraft” and a text field with class “other\_aircraft”. Then I  
pull in some javascript:

```
= javascript_include_tag 'aircraft_selection'

```

In public/javascripts/aircraft\_selection.js I clear the text field  
when a radio button is checked and clear the radio buttons when the  
text field receives input:

$(document).ready(function () {  
$(".our\_aircraft").change(function () {  
$(".other\_aircraft").val("");  
});

$(".other\_aircraft").keyup(function () {  
if ($(this).val() \> “”) {  
$(".our\_aircraft").removeAttr(“checked”);  
}  
});  
});

Enjoy!  
Bryan
