Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Cannot assign a function to JavaScript event; o_O.
Old 11-22-2007, 01:39 AM Cannot assign a function to JavaScript event; o_O.
stewart's Avatar
Novice Talker

Posts: 11
Name: Stewart Howe
Location: Denver, CO
Trades: 0
Alright, well I am trying to assign a function to a JavaScript Event for a retrieved element.

I will explain with code first :

Code:
window.onload = function() {

    var divBox = document.getElementById(someid)
    var divBox.onclick = someOtherFunction(arg,arg)

}

function someOtherFunction(arga,argb){
    var anotherDiv = document.getElementById(someid)
        anotherDiv.style.color='blue'
}
If I attempt to do something similar to the above code, it executes 'someOtherFunction' rather than just setting the onclick event to the function with the proper arguments that I need to send it.

Any suggestions ? Thanks a lot guys
__________________
stewart::howe
Web Developer & Programmer

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
| CelerLabs.com
stewart is offline
Reply With Quote
View Public Profile Visit stewart's homepage!
 
 
Register now for full access!
Old 11-22-2007, 11:22 PM Re: Cannot assign a function to JavaScript event; o_O.
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
In JS, functions are just another type of value. You assign the function itself as the event handler. You cannot pass arguments to it directly because that's an invocation.

A common technique is create a closure. That is, a function that calls the real function with the args you want.

Code:
window.onload = function() {

    var divBox = document.getElementById(someid)
    var divBox.onclick = function() { someOtherFunction(arg,arg); };

}
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Cannot assign a function to JavaScript event; o_O.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10084 seconds with 12 queries