Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Trouble with my javascript
Old 03-02-2004, 02:58 PM Trouble with my javascript
wicko3's Avatar
The Wickmeister

Posts: 368
Location: Derby, UK
Trades: 0
Hi guys.

I'm making my own javascript. Basically, I want a text box to clear, ready for user input, when the user clicks on it. Ideally, I'd like an if statement in there, so the text box is only cleared if it's still the default message there - i.e. I don't want the user's typing cleared when they re-click on it, you see.

This is what I've made so far:

In the head:
Code:
<script language="Javascript">
<!-- Script to clear initial text box values

function clearfriendname {

	if(document.refer.friendname.value="Friend's name"){
		document.refer.friendname.value=""
	}

}

function clearfriendemail {

	if(document.refer.friendemail.value="Friend's email"){
		document.refer.friendemail.value=""
	}

}

//-->
</script>
And in the body:
Code:
   <form name="refer">
    <p align="center"><input type="text" size="15" name="friendname"
    value="Friend's name" onclick="clearfriendname()"><br>
    <input type="text" name="friendemail" size="15" value="Friend's email"
    onclick="clearfriendemail()"><br>
    <input type="button" value="Send"></p>
   </form>
Unfortunately, it doesn't work. I get an 'object expected' error. I'm sure its just some simple Javascript programming standard that I'm getting wrong, or something like that. I'm afraid I've only learnt javascript from picking random chunks up over the years, so I don't have a particularly good knowledge of it, and the proper practices associated with it.

Thanks a lot,

Wicko
wicko3 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-02-2004, 04:58 PM Try This
Experienced Talker

Posts: 31
Location: Worchester, MA
Trades: 0
Just a quick modification of your script, also added script to change the values back if the user leaves w/o entering any information....

In JS, "=" sets the value, "==" is the comparison operator...

<script language="Javascript">
function clearfriendname(el) {
if(el.value=="Friend's name"){
el.value=""
}
}
function checkfriendname(el) {
if(el.value==""){
el.value="Friend's name"
}
}

function clearfriendemail(el) {
if(el.value=="Friend's email"){
el.value=""
}
}
function checkfriendemail(el) {
if(el.value==""){
el.value="Friend's email"
}
}
</script>
--------------------------------------------------------------------------------

And in the body:

code:--------------------------------------------------------------------------------
<form name="refer">
<p align="center"><input type="text" size="15" name="friendname"
value="Friend's name" onclick="clearfriendname(this)" onblur="checkfriendname(this)"><br>
<input type="text" name="friendemail" size="15" value="Friend's email"
onclick="clearfriendemail(this)" onblur="checkfriendemail(this)"><br>
<input type="button" value="Send"></p>
</form>
__________________

Please login or register to view this content. Registration is FREE

Last edited by JoeGoldberg; 03-02-2004 at 05:03 PM..
JoeGoldberg is offline
Reply With Quote
View Public Profile
 
Old 03-02-2004, 05:27 PM
wicko3's Avatar
The Wickmeister

Posts: 368
Location: Derby, UK
Trades: 0
Works a treat. Even better than I'd hoped! Now I come to think of it...I do remember something about == and = being different.

Thanks a lot mate,

Wicko
wicko3 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Trouble with my javascript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11956 seconds with 12 queries