Hi,
I m a new to JS and trying to get rid of value when someone click on text field but i get error which says "... is null" here is a screen from firebug.am i doing something wrong?
| SitePoint Sponsor |
Hi,
I m a new to JS and trying to get rid of value when someone click on text field but i get error which says "... is null" here is a screen from firebug.am i doing something wrong?

Hi there,
What the error is telling you is that the element you are trying to select (with the id of "Email-1361302728) doesn't exist on the page.
i.e. document.getElementById("Email-1361302728") is returning null.
If you want some help with this, post a link to a page where I can see this (or just make a JS Fiddle) and I don't mind having a look.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog
The problem i think is everytime the page load its a new field name as its created dynamically.how can i target them then.here u can find link to the page its private

Hi, there are a couple of ways, e.g. get a reference to a parent element and work from there.
Unfortunately, it's late here and I have to work tomorrow
If noone else has replied by tomorrow afternoon, I'll jump back in then.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

Hi there,
I managed to have a look at your problem before going to work.
It's actually very easy to solve.
You only have one form in the page. Get a reference to it like this:
Code JavaScript:f = document.getElementsByTagName('form')[0];
Once you've done this, you can reference the email field via its name (Email):
Code JavaScript:f.Email.onfocus = function(){ console.log("The input has recieved focus"); }
And there you go ...
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog
I tired it work but this isn't working
However i want something in if statement but i need this to work to i can add the if.not sure if i am doing it correct. Am i doing Email.Value right?Code JavaScript:var f = document.getElementsByTagName('form')[0]; f.Email.onfocus = function(){ Email.Value = ""; } var f = document.getElementsByTagName('form')[0]; f.Email.onfocus = function(){ Email.Value = ""; } var f = document.getElementsByTagName('form')[0]; f.Email.onfocus = function(){ Email.Value = ""; }

Hi there,
Everything is possible, no worries
The first thing to note is that value is case sensitive.
The second thing to notice is that inside the function, you still need the preceding reference to the form.
This will work:
Code JavaScript:f.Email.onfocus = function(){ f.Email.value = ""; }
This will not:
Code JavaScript:f.Email.onfocus = function(){ Email.Value = ""; }
If you need help with anything else, just let me know.
This will also work:
Code JavaScript:f = document.getElementsByTagName('form')[0]; f.Email.onfocus = function(){ this.value = ""; }
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog
the "this" is somewhat hard for me to grasp for some reason.i tried ur code and it work.of course it would ur a genius. However i am unable to get onblur to work Question : how does the code you wrote know the "Email" field and where it is as the id was changing as i mentioned above

Hi,
you have this:
Code JavaScript:var f = document.getElementsByTagName('form')[0]; f.Email.onfocus = function(){ if(f.Email.value = "email"){ f.Email.value = ""; } } f.Email.onblur = function(){ if(f.Email.value = ""){ f.Email.value = "email"; } }
This does not work as you are using a single equals to check for equality.
In JavScript (and most other programming languages) you use a double equals for this.
i.e. this:
Code JavaScript:if(f.Email.value = ""){
should be:
Code JavaScript:if(f.Email.value == ""){
In my code the this refers to the element which triggered the event your handler is dealing with (i.e. your email field).
The code knows where the email field is as it gets a reference to the form with this:
Code JavaScript:f = document.getElementsByTagName('form')[0];
Then uses the input's name attribute to attach an event handler to the field:
Code JavaScript:f.Email.onfocus = function(){...}
You can access the other fields in the same way, e.g. f.Name.value and f.Message.value
HTH
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

Stick it within two script tags and place it right before the closing body tags.
Somethin
Try and place this after wp_footer() if possible.HTML Code:<?php wp_footer(); ?> <script> var f = document.getElementsByTagName('form')[0]; f.Email.onfocus = function(){ if(f.Email.value == "email"){ f.Email.value = ""; } } f.Email.onblur = function(){ if(f.Email.value == ""){ f.Email.value = "email"; } } </script> </body> </html>
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog
Bookmarks