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.

JavaScript Forum


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



Reply
Old 06-14-2005, 12:34 PM string.replace probs
The_Anomaly's Avatar
Extreme Talker

Posts: 216
Location: Boston, Ma
Trades: 0
trying to make a text form. when someone fills out a box and click the text box (onBlur) if there was an '&' symbols it will replace it with 'and'. something i'm doing here is way wrong, i dun like javascript

Code:
<?xml version ="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>& - and</title>

<script type="text/javascript">   
	function replaceString(value)
		{
			var text1 = document.getElementById("text1"); 
			text1 = text1.replace("&","and");
		}
</script> 
	</head>



<body>
<form method=''post'' /> 
<input name="text1" type="text" id="text1" value="inputtext" onBlur="return replaceString(this);"> 
<input name="text2" type="text" id="text2" value="input more text"> 
<input name="Submit" type="submit" value="K"> </form>


</body>
</html>
The_Anomaly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-14-2005, 02:48 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
Code:
var text1 = document.getElementById("text1");
This will only get you a reference to the element on the page. It doesn't get you the 'value' property. To get/alter the value, you have to access it (like text1.value).

Therefore, the next line should be rewritten as:
Code:
text1.value = text1.value.replace("&","and");
This will work, but it will not replace all instances of the & (only one at a time). To fix this, change the search string into a regular expression where you can use the 'global' flag:
Code:
text1.value = text1.value.replace(/&/g,"and");
That line in place of yours should make it work as expected.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

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

Christopher is offline
Reply With Quote
View Public Profile
 
Old 06-14-2005, 03:08 PM
The_Anomaly's Avatar
Extreme Talker

Posts: 216
Location: Boston, Ma
Trades: 0
tried replacing that and it still doenst seem to work. am i doing something wrong with the onBlur ?
The_Anomaly is offline
Reply With Quote
View Public Profile
 
Old 06-14-2005, 04:00 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
It's working for me in Firefox and IE Make sure you're not using a cached version of the page.

And I didn't see you were using onblur with this. You don't need to use getElementById since this will already contain a reference to the element:
Code:
this.value = this.value.replace(/&/g,"and");
Note that the second text box won't replace the characters because you haven't attached any functionality to it.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

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

Christopher is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to string.replace probs
 

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.13427 seconds with 12 queries