Javascript problem: 'search' is undefined?

robing

New Member
The idea is that when a user focuses on the text box the 'Google Search' will disappear. This is the HTML<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN""http//www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http//www.w3.org/1999/xhtml"><head><title></title><link type="text/css" rel="Stylesheet" href="stylesheets\main.css" /><script src="script\main.js" type="text/javascript"></script></head><body><form method="get" action="http//www.google.ie/search"><p><input id="search" type="text" name="q" size="25" maxlength="255" value="Google Search"<br />
onfocus="searchFocus()" onblur="searchBlur()" /><input type="submit" value="Go" /></p></form></body></html><br />
<br />
This is the Javascript file<br />
<br />
function searchFocus() {<br />
if (search.value='Google Search')<br />
search.value=''<br />
}<br />
function searchBlur() {<br />
if (search.value='')<br />
search.value='Google Search'<br />
}<br />
<br />
What am I doing wrong?<br />
All help appreciated.<br />
I fogot to mention that when I open the page in Internet Explorer it gives me the error 'search' not defined<br />
Also I have tried changing the 'search.value' to 'this.value' in the javascript file.<br />
 

Silent

New Member
You're treating the word "search" like it's a variable name, but you haven't defined a variable with that name. If you're trying to get the input element with the id "search", you can't just say "search". You need to access it with the getElementById function.

Also, when comparing two values, you use the == operator, not =. The = operator is for assigning a value to a variable.

For example

if ( document.getElementById('search').value == 'Google Search' )

etc.
 
Top