Reply
Problem changing <input> background
Old 04-19-2008, 06:22 PM Problem changing <input> background
KkillgasmM's Avatar
Ultra Talker

Posts: 308
Name: El Phantasmo
Location: England, north west
I cant think why? It works with the first text field, but not with the second! They use the same script and almost identical event handlers!

Here is my HTML
Code:
    <form action="" method="post" name="form1" class="contact" id="form1">
        <p class="frmIntro">Feel free to message us through  our contact form and we'll get back to you as soon as possible.</p>
  <p>
          <label for="name">Name</label>
          <input name="name" type="text" id="name" size="30" maxlength="30" onfocus="active(name)" onblur="deActive(name)" />
        </p>
        <p>
          <label for="email">E-mail</label>
          <input name="email" type="text" id="email" size="30" maxlength="100" onfocus="active(email)" onblur="deActive(email)" />
      </p>

Here is my javascript
Code:
// makes a text field turn white on focus
function active(field)
    {
        var field = document.getElementById(field);
        field.style.backgroundColor="#FFF";
    }
    
// makes a text field turn light grey on blur
function deActive(field)
    {
        var field = document.getElementById(field);
        field.style.backgroundColor="#F2F2F2";
    }
When clicking on 'email', it doesnt change colour. 'name' works perfectly though?

Also, the error message that comes up in Firefox is 'field has no properties'

Any ideas whats wrong? Thanks
__________________
http://www.inspindesign.co.uk
KkillgasmM is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-19-2008, 07:02 PM Re: Problem changing <input> background
Christopher's Avatar
Iced Cap

Latest Blog Post:
Encoding Numbers as Base 36
Posts: 3,108
Location: Toronto, Ontario
Strings should be quoted, remember. I'm not sure why the name would work and not the email, but just try quoting the ID's you pass.

onfocus="active('email');"
onblur="deActive('email');"
__________________
Devlog - Latest PHP Article: MVC with the Zend Framework
::The New Tech - Technology Forum
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 04-20-2008, 05:02 AM Re: Problem changing <input> background
KkillgasmM's Avatar
Ultra Talker

Posts: 308
Name: El Phantasmo
Location: England, north west
Ahhh thanks thats fixed it! I have no idea why the name box was working properly though without the quotes?? Maybe because it was the first one in the form or something??

I ALWAYS get confused whether to pass quotes or not!
__________________
http://www.inspindesign.co.uk
KkillgasmM is offline
Reply With Quote
View Public Profile
 
Old 04-20-2008, 05:36 PM Re: Problem changing <input> background
Christopher's Avatar
Iced Cap

Latest Blog Post:
Encoding Numbers as Base 36
Posts: 3,108
Location: Toronto, Ontario
Not sure why the name worked. If you really want to know, try just alert(name) or something and see what the value is The only thing I can think of is if you had some other JS on the page that set a 'name' variable.

As for quotes, just remember that ALL strings must be quoted. There's no exception to the rule. If they aren't quoted then JS will think it's a variable. (And if the variable doesn't exist, then it will be undefined, which is why the code errored out).
__________________
Devlog - Latest PHP Article: MVC with the Zend Framework
::The New Tech - Technology Forum
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Reply     « Reply to Problem changing <input> background
 

Thread Tools

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

vB 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.13174 seconds with 13 queries