954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

textarea selection problems

Hello! =)

Please help me with this! =) This is my last one to my diploma work! =)

If the user enters a number in the textfield, than JS should set the selection in the textarea to that line. Empty lines are changed to " \n" coz I like for-in loops more... =)

function goToLine(num){  // gets a number from textfield
	if(num!=""){
		var e=document.getElementById("editor");
		e.value=e.value.replace(/\n/g, " \n");
		var eArray=e.value.split("\n");
		var sStart=0;
		if(num<=eArray.length){
			num=(num-1);
			for(x in eArray){
				//sLength=eArray[x].length;
				sStart=sStart+eArray[x].length;//sLength;
				if(x==num){
					e.value=e.value.replace(/\s{1}\n/g, "\n");
					e.setSelectionRange(sStart, 1);
					e.focus();
				}
			}
			alert(sStart);
		} else {
			e.value=e.value.replace(/\s{1}\n/g, "\n");
			alert("The number is bigger than the number of the lines! ("+eArray.length+")");
		}
	} else {
		alert("Enter a number to go line!");
	} //if num exists
}
benqus
Light Poster
39 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: