Question:

Javascript problem: 'search' is undefined?

by  |  earlier

0 LIKES UnLike

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/xhtml1...

<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"

onfocus="searchFocus()" onblur="searchBlur()" />

<input type="submit" value="Go" />

</p>

</form>

</body>

</html>

This is the Javascript file:

function searchFocus() {

if (search.value='Google Search')

search.value=''

}

function searchBlur() {

if (search.value='')

search.value='Google Search'

}

What am I doing wrong?

All help appreciated.

 Tags:

   Report

3 ANSWERS


  1. go to da java website


  2. You&#039;re treating the word &quot;search&quot; like it&#039;s a variable name, but you haven&#039;t defined a variable with that name. If you&#039;re trying to get the input element with the id &quot;search&quot;, you can&#039;t just say &quot;search&quot;. 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(&#039;search&#039;).value == &#039;Google Search&#039; )

    etc.

  3. define a form and put this inside it

    Try to learn form subject in javascript.

    &lt;input id=&quot;search&quot;  ......

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.