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
Problem with event handlers
Old 07-23-2008, 11:58 AM Problem with event handlers
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Im trying to create a script that has its own built in event handlers, so that I can keep the HTML separate from the behaviour.

I have to <a> tags, with separate id's, but I keep getting the 'has no properties' error message?

Here is my code!
Code:
// hide graphic thumbnails
function hideGraphic (portThumbGraphic)
    {
        var portThumbGraphic = document.getElementById ('portThumbGraphic');
        portThumbGraphic.style.display = 'none';
    }

// hide website thumbnails
function hideWebsite (portThumbWebsite)
    {
        var portThumbWebsite = document.getElementById ('portThumbWebsite');
        portThumbWebsite.style.display = 'none';
    }

// hide graphic thumbnails by default
window.onload = hideGraphic;

var btnThumbWebsite = document.getElementById ('btnThumbWebsite');
var btnThumbGraphic = document.getElementById ('btnThumbGraphic');

// set functions to nav list
btnThumbWebsite.onclick = hideGraphic;
btnThumbGraphic.onclick = hideWebsite;
Any ideas? Thanks to anyone who can help.
__________________

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 online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 07-23-2008, 12:18 PM Re: Problem with event handlers
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You need to set your events, and set the variables btnThumbWebsite, and btnThumbGraphic inside of a function initiated by the onload event. The reason that you are getting this error is that when you attempt to use getElementById, the elements are not available yet. By waiting for them to load, you will avoid this problem.

The other solution is to place the scripts down at the bottom, right before your closing </body> tag. This will insure that all of the HTML is loaded before the script is run. The upside of this method is that the DOM is available, but you don't need to wait for images to load, unlike the onload event. Be aware, however, that scripts run in this fashion do not wait for images to be ready before they run, which will be important if you ever need to do something to images.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 07-23-2008 at 12:20 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 07-23-2008, 01:12 PM Re: Problem with event handlers
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Thanks a lot.

My code now reads like so:

Code:
function hideGraphic (portThumbGraphic)
    {
        var portThumbGraphic = document.getElementById ('portThumbGraphic');
        var portThumbWebsite = document.getElementById ('portThumbWebsite');
        portThumbGraphic.style.display = 'none';
        portThumbWebsite.style.display = 'block';
    }

// hide website thumbnails
function hideWebsite (portThumbWebsite)
    {
        var portThumbWebsite = document.getElementById ('portThumbWebsite');
        var portThumbGraphic = document.getElementById ('portThumbGraphic');
        portThumbWebsite.style.display = 'none';
        portThumbGraphic.style.display = 'block';
}

function pageLoad ()
    {
        var btnThumbWebsite = document.getElementById ('btnThumbWebsite');
        var btnThumbGraphic = document.getElementById ('btnThumbGraphic');
        
        // set functions to nav list
        btnThumbWebsite.onclick = hideGraphic;
        btnThumbGraphic.onclick = hideWebsite;
        
        // hide graphic thumbnails by default
        hideGraphic ();
    }

window.onload = pageLoad;
But Im wondering, is there anyway to stop the page from refreshing after clickin on the <a> tag?

Ive had to change the anchor tags to <span> to prevent this, but I dont like the way the text cursor appears as it stops the buttons from looking 'clickable'.
__________________

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 online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 07-23-2008, 02:03 PM Re: Problem with event handlers
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Never mind, I used CSS to sort it out, didnt expect it to be that easy
__________________

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 online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 07-23-2008, 03:11 PM Re: Problem with event handlers
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You can have the href in the anchor tags be anything you want, and as long as the function that handles your click event returns false, it won't be followed, thus no refresh.
__________________
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-23-2008, 03:53 PM Re: Problem with event handlers
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
I see! So does 'return false' basically just stop something being submitted / refreshed? I thought it meant all sorts of things, lol
__________________

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 online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 07-23-2008, 04:01 PM Re: Problem with event handlers
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Correct. It would also, for example, stop the page from jumping to a named anchor, or prevent a form from being submitted.

It also returns a value (false), which is reflected in any variable that is assigned the value of the function.
__________________
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-23-2008, 06:28 PM Re: Problem with event handlers
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
I get ya, thanks
__________________

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 online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 07-24-2008, 05:21 AM Re: Problem with event handlers
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Ive had to change the anchor tags to <span> to prevent this, but I dont like the way the text cursor appears as it stops the buttons from looking 'clickable'.
To any future readers;

the CSS rule to stop this is;
Code:
cursor:pointer;
__________________
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 offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Problem with event handlers
 

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