Member Avatar for Aardwolf

I don't know how to do this for Javascript, but I can explain in PHP as I have further experience in it. maybe someone can translate.

A textbox's value is "username", and it needs a onclick javascript code that makes it 'this.value = ""'

When users click the textbox that has the value of "username", it by javascript turns the value to "" (empty), how do i make it that it will only change the value to blank if the value is "username". not say... "admin".
So if they made changes to the textbox value to "admin" and they click the textbox again, it will stay not replace the value by ""(blank).

<?php

$usertext = $_GET['username'];

if($usertext == "username") {
$usertext = "";
} else {
return false;
}

?>

Recommended Answers

All 8 Replies

You will need pure javascript, not java.
Have a button. when the button clicks, execute a function that takes the value of the text field.
If the values is "username" make it empty: (value="")

Check out the tutorials form the W3Schools site about javascript

Member Avatar for Aardwolf

I never asked about Java.

I also want it to make it onclick on the textbox, not a button.

When they click the textbox, if the value is "username" it will empty it for another value, else no changes happens.

<script lang='javascript'>
function makeBlank(obj,defMsg){
	if(obj.value==defMsg){
		obj.value="";
	}
}
function fillDefValue(obj,defMsg){
	if(obj.value==""){
		obj.value=defMsg;
	}
}
</script>

<input style="width:190px" onblur="fillDefValue(this,'User Name')" onfocus="makeBlank(this,'User Name')" value="Name" name="fromname" id="fromname" type="text">

This is corrected one

<script lang='javascript'>
function makeBlank(obj,defMsg){

	if(obj.value==defMsg){
		obj.value="";
	}
}
function fillDefValue(obj,defMsg){
	if(obj.value==""){
		obj.value=defMsg;
	}
}
</script>

<input name="fromname1" id="fromname1" type="text"> <br>
<input style="width:190px" onblur="fillDefValue(this,'User Name')" onfocus="makeBlank(this,'User Name')" value="User Name" name="fromname" id="fromname" type="text">

I never asked about Java.

I also want it to make it onclick on the textbox, not a button.

When they click the textbox, if the value is "username" it will empty it for another value, else no changes happens.

You posted in JSP, that stands for Java Server Pages, which is miles away from JavaScript. Make sure you know where you posting next time. Post moved!

Member Avatar for Aardwolf

Sorry about that
---

I'm looking more for an onclick function

blur and onfocus is best to use, but still you can do

onclick="makeBlank(this,'User Name')"
Member Avatar for Aardwolf

Thank you, haven't tried it, but looks great.

I'll try and fit the function in that onclick. Not a separate function.

Much thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.