0

Hi,

Code below replaces selected number with an URL given by the user. It doesn't work the way it supposed to do. If I select second 2 to make it an URL, it makes first 2 an URL instead. How do I solve this problem?

Thanks in advance

<script language="JavaScript">
function makeURL(content){
	var selected = (content.value).substring(content.selectionStart, content.selectionEnd);
	var url = prompt("Please insert URL here:");
	content.value = content.value.replace(selected, url);
}
</script>

<form name="form1" action="next.php" action="POST">
	<textarea name="textareaContent" rows="10" cols="50">1, 2, 3, 2, 1.</textarea>
	<br />
	<input type="submit" name="submitButton" value="SEND" />
</form>

<br />
<a href="#" onClick="makeURL(form1.textareaContent)">Make it URL</a>
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by veledrom
0

try:

content.value = content.value.substring(0,content.selectionStart) + url + content.value.substring(content.selectionEnd);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.