Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine two javascript functions??

Status
Not open for further replies.

irinapl

Programmer
Aug 30, 2006
5
NO
Is it possible to combine two JavaScritpt functions?

I want to "automatically" add a function to all input fields. But origianl function sould also be called.

Example: I have a following field:

<input onchange="alert('hi ')">




Then I want to add a call to

function sayHello(){
alert('hello')
}

The call to function sayHello() will be added from body onload function.

I want both "hi" and "hello" to be displayed.

 
I found out:

here is the code:

function addOnClickFunction(elem, newFunction){
var orig = elem.onclick;
elem.onclick = function(){
newFunction();
if(orig){
orig();
}
}
}
 
You beat me to it.
But of course there is more to the code.
You have to retrive and loop through the collection of form elements and test to see if the particular element is of a type that you want to modify or not. Also you might need to consider if it is always an onclick event you are looking for or if you want to watch for onchange, onkey or onmouse events and modify them rather than just looking for onclick.

It all depends of course on your purpose for doing this but you also might want to intercept hyperlinks to execute your code before changing the page.


At my age I still learn something new every day, but I forget two others.
 
also look into the DOM way of doing it.
ie attacheEvent (IE) and addEventListener(Firefox browsers)...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top