homepage Welcome to WebmasterWorld Guest from 54.196.195.207
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
onfocus and onblur events don't work correctly
ktsirig




msg:3986618
 1:44 pm on Sep 9, 2009 (gmt 0)

Hello,
I have a page with a textbox that I want to have a message written inside it that will dissapear when the user clicks in the text box and writes something and it will show up again if the user clicks somewhere else but hasn't written anything inside the textbox. So I am using the onfocus event in order to write "Enter your email here" and the onfocus event in order to show the "Enter your email here" message inside the textbox if the user clicks somewhere else in the webpage but has left the textbox blank. If however the user has written, for example "jim@yahoo.com", I want this to remain in the textbox.
What am I doing wrong?


<html>
<head>
<script language="javascript" type="text/javascript">

function clearPassword()
{
document.getElementById('sample').setAttribute('class', 'empty');
document.getElementById('sample').value = '';
}

function restorePassword()
{
var given_text= document.getElementById('sample').value;

if (given_text='')
{
document.getElementById('sample').setAttribute('class', 'grayedOut');
document.getElementById('sample').value = 'Enter your email';
}
else
{
document.getElementById('sample').setAttribute('class', 'empty');
document.getElementById('sample').value = given_text;
}
}
</script>
</head>
<body>
<form >
<input id="sample" type="text" value="Enter your email" size="22" class="grayedOut" onfocus="javascript:clearPassword();" onblur="javascript:restorePassword();"/>
<input id="new" type="text"/>
</form>
</body>
</html>

 

daveVk




msg:3987057
 2:42 am on Sep 10, 2009 (gmt 0)

change
if (given_text='')
to
if (given_text==='')

also setAttribute('class', ... ) is problem with some browsers

function restorePassword() {
var el = document.getElementById('sample');
if (el.value === '') {
el.className = 'grayedOut';
el.value = 'Enter your email';
} else { el.className = 'empty'; } // is else required ?
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved