cdcrazy2292
cdcrazy2292

Reputation: 49

Clearing textfield with javascript or html itself

this might be a silly question but i have worked on this for quite a bit and i still can't figure it out. I am creating a form in html that contains a table. Inside a cell from this table, there is a textfield that has "Name" by default. I would like to make "Name" Disappear as soon as I click on the text field.

Here's my row:

<html>
<script type="text/javascript" src="on_click.js"></script>
....
....
<tr>
<td></td>
<td><input type="text" name="addname" value="Name" size="179" onclick="Clear()" id="textbox1"></td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="lastname" value="Last" size="179"     onclick="clear() id="textbox2"></td>
<td></td>
</tr>
....
</html>

The following is my javascript code:

function Clear()
{  
document.getElementById("textbox1").value= "";
document.getElementById("textbox2").value= "";

}

I don't know if maybe i am using the wrong function, or if I'm not including my files correctly inside my javascript or my html. I'm new to javascript and i would greatly appreciate it if someone can help me out with this.

Thank you!!

Upvotes: 1

Views: 99

Answers (2)

OHNH
OHNH

Reputation: 84

Run .js script after the tags loading.

<html>

....
....
<tr>
<td></td>
<td><input type="text" name="addname" value="Name" size="179" onclick="Clear()" id="textbox1"></td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="text" name="lastname" value="Last" size="179"     onclick="clear()" id="textbox2"></td>
<td></td>
</tr>
<script type="text/javascript" src="on_click.js"></script>
....
</html>

You can also try this:

onclick="this.value=''"

Upvotes: 0

Raymond Ativie
Raymond Ativie

Reputation: 1826

why not just put a placeholder in the textbox instead Example <input type='text' name='name' placeholder='Name' />

This is a browser compatible way of doing what you want

Upvotes: 2

Related Questions