benqus 0 Light Poster

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
}
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.