Original Post
I am completely new to programming in Javascript (I have started a few of days ago actually lol). Well, I am trying to create a "placeholder text" for each of my "input fields", and as you can see I have created a separate Javascript Function for each "onblur code snippet", "onfocus code snippet", etc. My problem is that IF I would want to add/create more "input fields", than I would have to create more Functions for each different section: "onblur, onfocus, etc." as I have done for my First Name and Last Name "input fields", which will than make my Javascript code too long and very burden...
My question is: how can I combine the "placeholder text" Javascript Functions that I have inserted in to the "head" section in to one Javascript Function, so that only one Function can execute the "placeholder text" Javascript code on all the different "input fields" that I would like to create, to make my Javascript code look more simpler and neat? Thanks guys!
Example, one Javascript Function that will execute all the different placeholder text for:
- A placeholder text for First Name
- A placeholder text for Last Name
- A placeholder text for Email
- A placeholder text for password
etc, etc...
My question is: how can I combine the "placeholder text" Javascript Functions that I have inserted in to the "head" section in to one Javascript Function, so that only one Function can execute the "placeholder text" Javascript code on all the different "input fields" that I would like to create, to make my Javascript code look more simpler and neat? Thanks guys!
Example, one Javascript Function that will execute all the different placeholder text for:
- A placeholder text for First Name
- A placeholder text for Last Name
- A placeholder text for Email
- A placeholder text for password
etc, etc...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript Practice</title>
<script type="text/javascript">
//For the first input field: "First Name"
function fnameBlur(x)
{
document.getElementById(x).value='First Name';
}
function fnameFocus(x)
{
document.getElementById(x).value='';
}
//For the second input field: "Last Name"
function lnameBlur(x)
{
document.getElementById(x).value='Last Name';
}
function lnameFocus(x)
{
document.getElementById(x).value='';
}
</script>
</head>
<body>
First name: <input type="text" onblur="fnameBlur(this.id)" onfocus="fnameFocus(this.id)" id="fname" value="First Name" />
Last name: <input type="text" onblur="lnameBlur(this.id)" onfocus="lnameFocus(this.id)" id="lname" value="Last Name" />
</body>
</html>