# How to use a javascript funtion

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-javascript-funtion/126899>\
**Category:** Rails\
**Created:** [January 23, 2008, 8:16am UTC](https://www.ruby-forum.com/t/how-to-use-a-javascript-funtion/126899 "2008-01-23T08:16:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [January 23, 2008, 8:16am UTC](https://www.ruby-forum.com/t/how-to-use-a-javascript-funtion/126899/1 "2008-01-23T08:16:08Z")

</div>

how to use a javascript funtion which should run when i am clicking any  
link or submit in whole form?  
except in onclick tag (or) on submit tag

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [January 23, 2008, 10:37am UTC](https://www.ruby-forum.com/t/how-to-use-a-javascript-funtion/126899/2 "2008-01-23T10:37:28Z")

</div>

Pokkai D. wrote:

> how to use a javascript funtion which should run when i am clicking any  
> link or submit in whole form?  
> except in onclick tag (or) on submit tag

don’t get, what exactly you want to do

link\_to\_function “Greeting”, “alert(‘Hello world!’)”  
would create a link to a js function

otherwise you can implement onclick whereever necessary:  
onclick=“alert(‘Hello world!’);return false;”

or combine it with onsubmit etc.

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [January 24, 2008, 6:11am UTC](https://www.ruby-forum.com/t/how-to-use-a-javascript-funtion/126899/3 "2008-01-24T06:11:59Z")

</div>

Thorsten M. wrote:

> Pokkai D. wrote:
> 
> > how to use a javascript funtion which should run when i am clicking any  
> > link or submit in whole form?  
> > except in onclick tag (or) on submit tag
> 
> don’t get, what exactly you want to do
> 
> link\_to\_function “Greeting”, “alert(‘Hello world!’)”  
> would create a link to a js function
> 
> otherwise you can implement onclick whereever necessary:  
> onclick=“alert(‘Hello world!’);return false;”
> 
> or combine it with onsubmit etc.

i want to do if there is any button or link is clicked then a javascript  
function should automatically execute

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [January 24, 2008, 9:00am UTC](https://www.ruby-forum.com/t/how-to-use-a-javascript-funtion/126899/4 "2008-01-24T09:00:10Z")

</div>

I think the prototype equivalent is

$$(‘a’).onclick = function(){  
}

however I am also more partial to the JQuery way. I have yet to have a  
problem making them work together.

Just get this order right and you should be fine:

include jQuery  
call “var J = jQuery.noConflict()”  
include prototype

J(‘a’).click(function(){})

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [January 24, 2008, 8:36am UTC](https://www.ruby-forum.com/t/how-to-use-a-javascript-funtion/126899/5 "2008-01-24T08:36:18Z")

</div>

Pokkai D. wrote:

> Thorsten M. wrote:
> 
> > Pokkai D. wrote:
> > 
> > > how to use a javascript funtion which should run when i am clicking any  
> > > link or submit in whole form?  
> > > except in onclick tag (or) on submit tag
> > 
> > don’t get, what exactly you want to do
> > 
> > link\_to\_function “Greeting”, “alert(‘Hello world!’)”  
> > would create a link to a js function
> > 
> > otherwise you can implement onclick whereever necessary:  
> > onclick=“alert(‘Hello world!’);return false;”
> > 
> > or combine it with onsubmit etc.
> 
> i want to do if there is any button or link is clicked then a javascript  
> function should automatically execute

simple with jQuery !

jQuery(‘a’).click(function(){  
my nice code here  
});

although, getting jQuery working with prototype is bit tricky. i use  
jrails javascript files, but WITH prototype.
