Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Javascript Replace Function.


  • Please log in to reply

#1
IO-error

IO-error

    Member

  • Member
  • PipPipPip
  • 276 posts
Hi there fellow-geeks.

I got a problem, which I am unable to solve by myself.
I followed tutorials and asked some people that know quite a lot about javascript.

They couldn't help me, sadly enough.

I tried several things, they are prefixed with #1 and #2. To make clear which attempt is which code.


Header:
<script language="javascript">
<!--
function highlight(area){
document.form_code(area).focus()
document.form_code(area).select()
}
-->
</script>

Script:
<script type="text/javascript">
<!--
function getSelText()
{
	var txt = '';
	var foundIn = '';
	if (window.getSelection)
	{ 
	 
		txt = window.getSelection();
		 
		 
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
		 
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
		 
	}
	else return;

#1	s = document.aform.selectedtext.value;
#1	document.write(s);
#1	s = s.replace(/Chr(9)/g,"\r");
#1	document.aform.selectedtext.value = s;

#2	document.aform.selectedtext.value = replace(document.aform.selectedtext.value, /Chr(9)/, "\r")

	document.aform.selectedtext.value = document.aform.selectedtext.value + "\r" + txt;
}
// -->
</script>


The code without prefix numbers is a working piece of code.
If you need more information about the script before you can help me, feel free to PM or, if possible, mail me.
  • 0

Advertisements


#2
Diego8

Diego8

    Member

  • Member
  • PipPipPip
  • 189 posts
What are you trying to acomplish?
Could you explain a little bit more?
  • 0

#3
IO-error

IO-error

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 276 posts
Of course.

I want to select some text and click that button.
When I click the button, javascript reads the text I selected.
That is already working.

But I want to change the value that he has read.
I want to take a tab, which might occure in a selection, and replace it with \r.

How would I actually change the inside of a variable, that is saved in an objectstructure?
  • 0

#4
Diego8

Diego8

    Member

  • Member
  • PipPipPip
  • 189 posts
the string "object" has a replace method built-in.
An example of this function would be:

var str = 'this is a test';
document.write(str.replace("is","was"));

that would print "this was a test".
I think you can also use a regular expression as search pattern.
This web page may come in handy http://www.w3schools...ref_replace.asp

Edited by Diego8, 25 June 2008 - 06:09 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP