0

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;
}

?>

Edited by peter_budo: Keep It Clear - Do use code tags when posting any code

4
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by Voltimolt
0

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

Edited by javaAddict: n/a

-1

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.

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

Edited by urtrivedi: n/a

0

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

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!

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.