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
Setting event handlers dynamically
Old 07-31-2008, 04:30 PM Setting event handlers dynamically
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Is it wrong to include parenthesis when you do this? As the function I am coding could really do with the parenthesis being there.

Would something like the following be invalid?

Code:
image.onmouseover = myFunction (myVariable[i]);
I think its that part of my code that is preventing my script from functioning properly anyway. Although no errors show up in the error console?
__________________

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

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 07-31-2008, 05:06 PM Re: Setting event handlers dynamically
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
There doesn't look to be anything wrong with that. Any variable that returns a value can be used in the place of any string or number, as long as you are careful about the distinction between addition (+) and concatenation (+).
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 07-31-2008, 05:54 PM Re: Setting event handlers dynamically
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It does looks wrong to me...
What you should give here, is just the name of the function to be used. When that function is called, it's like an "eval".
The event can fire synchronously, but it could by asynchronous too. In the last case, the value of myVariable can be different than the one in your page.

You have to handle that into the function itself.
And remember that a function in javascript can receive an parameter, even if it's not declared.
In the case of an event, you will receive a "this" variable in your function that will be related to the element that have triggered the event.
http://developer.mozilla.org/en/docs...in_the_handler

So, the proper way (if I want to be extrem) would even be:
http://developer.mozilla.org/en/docs...dEventListener
Code:
try{
  //for anything except IE
  image.addEventListener('mouseover',myFunction,false);
}
catch e{
  //for IE
  image.attachEvent('onmouseover',myFunction);
}

function myFunction(){
  var myVariable=this.some_property.value;
  alert myVariable;
}
or:
Code:
image.onmouseover=myFunction;

function myFunction(){
  var myVariable=this.some_property.value;
  alert myVariable;
}
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 07-31-2008 at 05:56 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-31-2008, 06:43 PM Re: Setting event handlers dynamically
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
image.onmouseover = function() {myFunction (myVariable[i]);};
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-31-2008, 07:10 PM Re: Setting event handlers dynamically
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Thanks a lot for the tips! To anyone who is interested, I sorted it with the following
Code:
// display the name of the project on thumb hover

function thumbChange ()    
    {
        // set pWebsite equal to image title on hover
        switch (this.parentNode.parentNode.parentNode.id)
            {
                case 'portThumbWebsite':
                paragraph = document.getElementById ('pWebsite');
                break;
                
                case 'portThumbGraphic':
                paragraph = document.getElementById ('pGraphic');
                break;
            }
        
        paragraph.firstChild.nodeValue = this.title;
    }
    
function thumbAttr (portThumb)
    {
        img = document.getElementById (portThumb).getElementsByTagName ('img');
        
        for (i = 0; i < img.length; i ++) 
            {
                img[i].onmouseover = thumbChange;
            }
    }
__________________

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

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 07-31-2008, 10:18 PM Re: Setting event handlers dynamically
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Argggh... ur both right... I wasn't paying attention this time...
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Setting event handlers dynamically
 

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.71084 seconds with 12 queries