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>

Recommended Answers

All 2 Replies

try:

content.value = content.value.substring(0,content.selectionStart) + url + content.value.substring(content.selectionEnd);

Excellent.

Thanks hielo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.