Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-01-2009, 11:11 AM   PM User | #1
viscep
New to the CF scene

 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
viscep is an unknown quantity at this point
Count Characters in multiple form fields

I am having problems counting the characters as a user types in multiple fields in a form. It should add both fields together and output the total. The best I have been able to do so far is get it to count the first field, but when I type in the second field, it does not update until I type in the first field again. All help is much appreciated


Code:
function countChar(title,message,f)
{	
	f.count.value = title + message;
}


<input type='text' name='title' onkeyup="countChar(this.value.length,message.value.length,this.form)">
		
<textarea name='message'  onkeyup="countChar(this.value.length,title.value.length,this.form)" ></textarea>

You've typed <input size='3' type="text" name="count" value="0"> characters
viscep is offline   Reply With Quote
Old 01-01-2009, 11:55 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Code:
<script type="text/javascript">
function countChar(title,message,fObj)
{	
	fObj.count.value = title + message;
}

</script>
<form onkeyup="countChar(this.title.value.length,this.message.value.length,this);">
<input type="text" name="title" >
		
<textarea name="message" ></textarea>

You've typed <input size="3" type="text" name="count" value="0"> characters
</form>
PS: always use double quotes to wrap attribute values ,to make your markup valid one.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 01-01-2009, 06:11 PM   PM User | #3
viscep
New to the CF scene

 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
viscep is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
Code:
<script type="text/javascript">
function countChar(title,message,fObj)
{	
	fObj.count.value = title + message;
}

</script>
<form onkeyup="countChar(this.title.value.length,this.message.value.length,this);">
<input type="text" name="title" >
		
<textarea name="message" ></textarea>

You've typed <input size="3" type="text" name="count" value="0"> characters
</form>
PS: always use double quotes to wrap attribute values ,to make your markup valid one.

Thank you for the help. It works fine using Internet Explorer but it does not work with firefox. Any idea how to make it work with firefox? Thank you very much
viscep is offline   Reply With Quote
Old 01-02-2009, 06:35 AM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
I had tested it only in FF(2), and that worked for me. (It works in IE too)
Quote:
It works fine using Internet Explorer but it does not work with firefox.
Could you explain "does not work " ? Are you getting any errors?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 01-02-2009, 11:20 AM   PM User | #5
viscep
New to the CF scene

 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
viscep is an unknown quantity at this point
Quote:
Originally Posted by abduraooft View Post
I had tested it only in FF(2), and that worked for me. (It works in IE too)
Could you explain "does not work " ? Are you getting any errors?
I'm using FF3. In firebug it says prototype undefined. It doesn't count any characters, the count value stays at 0.
viscep is offline   Reply With Quote
Old 01-06-2009, 08:08 AM   PM User | #6
viscep
New to the CF scene

 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
viscep is an unknown quantity at this point
Here is the main piece of code, it does not work for Firefox or Opera


Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<script type="text/javascript">

function countChar(title,message,f)
{	
	var t = title;
	var m = message;
	f.Count.value = "$" + (t + m) * 50;
}

</script>

<form method="post" onkeyup="countChar(this.title.value.length,this.message.value.length,this);">
	<tr>
		<td width="15%"><b>Title</b>:</td>
		<td width="85%">
		<input type="text" name="title" size="35" maxlength="30">
		</td>
	</tr>

	<tr>
		<td width="15%"><b>Message</b>:</td>
		<td width="85%">
		<textarea name="message" cols="55" rows="6" ></textarea>
		</td>
	</tr>

	<tr>
		<td width="15%"><b>Cost</b>:</td>
		<td width="85%">
		<input size="5"  name="Count" value="0">
		<input style="margin-left:10px;" type="submit" name="ad" value="Submit"></td>
	</tr>
	</form>
Thanks for your help
viscep is offline   Reply With Quote
Old 01-06-2009, 08:13 AM   PM User | #7
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
There might another issue: title could be a reserved javascript word (see: document.title) in some browsers. Change the name title in Title, or whichever.

Another thing: onkeyup should not be supported by a FORM element (AFAIK). Maybe you could use onsubmit and return a Boolean.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 01-06-2009 at 09:28 AM.. Reason: title, onkeyup
Kor is offline   Reply With Quote
Old 01-06-2009, 08:32 AM   PM User | #8
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Another thing: onkeyup should not be supported by a FORM element (AFAIK). Maybe you could use onsubmit and return a Boolean.
I don't think so. It might be due to the errors in OP's markup. Validate it and fix them.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 01-06-2009, 08:33 AM   PM User | #9
tagnu
Regular Coder

 
Join Date: Nov 2007
Location: 127.0.0.1
Posts: 348
Thanks: 26
Thanked 40 Times in 39 Posts
tagnu will become famous soon enough
Works fine in Firefox 3.0.5

Quote:
Originally Posted by viscep View Post
Here is the main piece of code, it does not work for Firefox or Opera
__________________
Blog Charity:Water
WhatisWrongWith.me/tagnu - Send me anonymous feedback.
tagnu is offline   Reply With Quote
Old 01-06-2009, 05:26 PM   PM User | #10
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

function countChar()
	{
	var tform = document.getElementById("my_form");
	tform.count.value = tform.title.value.length*1+tform.message.value.length;
	}

// ]]>
</script>
</head>
<body>

<form id="my_form" onkeyup="countChar();">
	<input type="text" name="title" />
	<textarea name="message"></textarea>
	You've typed <input size="3" type="text" name="count" value="0" /> characters
</form>

</body>
</html>
Passing parameters into an event handler that is supposed to also pass the Event object (like onkeyup) can be finicky because the first parameter (arguments[0]) can be Event (as opposed to whatever parameter you think you're passing). I've never had a problem with this before because afaik dev parameters take precedence - but I could be wrong, just a thought.
__________________
Feel free to e-mail me if I forget to respond ;)
ohsosexybrit@gmail.com

Last edited by itsallkizza; 01-06-2009 at 05:29 PM..
itsallkizza is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:17 AM.


Advertisement
Log in to turn off these ads.