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
onload problem with form <input> field
Old 08-25-2008, 06:16 AM onload problem with form <input> field
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
I have a script that will loop through each input and textarea field on a page and set onfocus and onblur events to change the background colour.

Here is some of the code that does it

Code:
                                elementInput[j].onfocus = active;
                                elementInput[j].onblur = deActive;
                                elementInput[j].onload = deActive;
Here are the functions that are called to do this:
Code:
// makes a text field turn white on hover
function active ()
    {
        var field = document.getElementById (this.id);
        field.style.backgroundColor="#FFF";
    }
    
// makes a text field turn light grey on blur
function deActive ()
    {
        var field = document.getElementById (this.id);
        field.style.backgroundColor="#F9F9F9";
    }
However, the
Code:
elementInput[j].onload = deActive;
Part of the code does not work. It doesnt create an error, but it just doesn't do anything! Its supposed to make all the input fields turn grey using the deActive function, but nothing seems to happen?
__________________

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 08-25-2008, 08:52 PM Re: onload problem with form <input> field
Extreme Talker

Posts: 238
Location: United States
Trades: 0
Quote:
Originally Posted by KkillgasmM View Post
I have a script that will loop through each input and textarea field on a page and set onfocus and onblur events to change the background colour.

Here is some of the code that does it

Code:
                                elementInput[j].onfocus = active;
                                elementInput[j].onblur = deActive;
                                elementInput[j].onload = deActive;
Here are the functions that are called to do this:
Code:
// makes a text field turn white on hover
function active ()
    {
        var field = document.getElementById (this.id);
        field.style.backgroundColor="#FFF";
    }
    
// makes a text field turn light grey on blur
function deActive ()
    {
        var field = document.getElementById (this.id);
        field.style.backgroundColor="#F9F9F9";
    }
However, the
Code:
elementInput[j].onload = deActive;
Part of the code does not work. It doesnt create an error, but it just doesn't do anything! Its supposed to make all the input fields turn grey using the deActive function, but nothing seems to happen?
My understanding is that the onload event is only valid on certain elements, such as body, img, and iframe (i.e., elements that need to be loaded). I don't think onload would work on an input field.

If you are trying to make the default background-color of that element to be a certain color, then it is better to set it in the CSS than JavaScript.
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
frost is offline
Reply With Quote
View Public Profile
 
Old 08-26-2008, 08:13 AM Re: onload problem with form <input> field
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
input fields and textareas are NOT created by HTML. They are created by the underlying OS of the client browser and as such "on the fly" style changes will often have no effect.

use document.form.element.disabled = true|false and let the OS take care of the input state
__________________
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!
 
Old 08-26-2008, 03:00 PM Re: onload problem with form <input> field
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Quote:
Originally Posted by frost View Post
If you are trying to make the default background-color of that element to be a certain color, then it is better to set it in the CSS than JavaScript.
Yeah I basically set the CSS to make the fields white, that is how I want them to look if JavaScript is disabled. If it is enabled however, it makes them all grey (or is supposed to!) then makes them white when focused. Thanks anyway!

Quote:
Originally Posted by chrishirst View Post
use document.form.element.disabled = true|false and let the OS take care of the input state
Thanks but I don't really have any idea what you mean. What would the 'disabled' part of the code do? Is there any way I could assign the function to the form element similar to how Ive tried already? hmmmmmm!
__________________

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 08-26-2008, 03:12 PM Re: onload problem with form <input> field
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
What would the 'disabled' part of the code do?
setting the disabled property of a form element to true, is the equivalent of using disabled="disabled" in HTML source
It will "grey it out" and stop it receiving focus.

Quote:
Is there any way I could assign the function to the form element similar to how Ive tried already
Yep, just make your active / inactive functions set the disabled property instead of the style.backgroundColor property.
__________________
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 onload problem with form <input> field
 

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