Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

focus on textarea in form

Status
Not open for further replies.

RZillmer

Programmer
Nov 20, 2001
42
US
I have a textarea on a form. I have javascript buttons above it to add html tags (i.e. <b>, </b>) to it. I have two problems.

I'd like to be able to add the tag wherever the cursor is in the text area instead of appending it to the end. If this is not simple, it's really not important.

More importantly, I'm trying to get the focus back to the textarea after adding the tag. If it has to be putting the cursor at the end of the text, so be it. as of right now I can't even get the focus back to anywhere in the textarea. Here is the code I'm using:
Code:
function Bold(textBoxNumber)
{
	selectedText = false;

	if (browserVersion >= 4)
	{
		selectedText = document.selection.createRange().text;
	}

	if (selectedText)
	{
		// Add tags around selection
		document.selection.createRange().text = &quot;<b>&quot; + selectedText + &quot;</b>&quot;;
		eval('document.submissionForm.paragraph'+textBoxNumber+'.focus()');	THIS LINE IS NOT WORKING 10/02/03
		selectedText = '';
		return;
	}
	else
	{
		if (eval('document.submissionForm.boldButton'+textBoxNumber+'.value == &quot; B &quot;'))
		{
			eval('document.submissionForm.boldButton'+textBoxNumber+'.value = &quot; B* &quot;');
			eval('document.submissionForm.paragraph'+textBoxNumber+'.value += &quot;<b>&quot;');
		}
		else
		{
			eval('document.submissionForm.boldButton'+textBoxNumber+'.value = &quot; B &quot;');
			eval('document.submissionForm.paragraph'+textBoxNumber+'.value += &quot;</b>&quot;');
		}
		return;	
	}
}
I have multiple textareas in the one form (submissionForm), and they are named paragraph1, paragraph2, and so on. There is a bold button for each, and they are named boldButton1, boldButton2, and so on. My onclick is Bold(1), Bold(2), and so on.

Thanks in advance for any help.
 
Try:

eval('document.submissionForm.paragraph'+textBoxNumber+'.focus');

Leave off the &quot;()&quot; at the end of focus.
 
That didn't work.

I even tried just a basic
document.submissionForm.paragraph1.focus;

That didn't work either. Then I tried
document.submissionForm.title.focus;

title is just a textbox, not a textarea. That still didn't work. Focus just remains on the button that was pressed. I can't figure it out.
 
the function focus() does need the () following it.
eval('document.submissionForm.paragraph'+textBoxNumber+'.focus()');
is the correct suntax for setting the focus to the textarea you however cannot as far as I know set the focus to a specific line in the textarea. that's what I gather you are trying to do.
so the correct syntax would be
eval('document.submissionForm.[itextarea name[/i].focus()'); A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
don't know what happen to that one.
eval('document.submissionForm.textarea name.focus()'); A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
All of those hard-coded statements I mentioned, I tried with and without ().

I can't for the life of me figure out what is wrong. All of my other evals and changes to properties on my form objects are working except for this focus() one.
 
Ok, I'll give it one last shot.

Try:

function setFocus(pNum){
var Test;
Test=document.getElementById(eval('testTA'+pNum));
Test.focus();

}

The focus(cursor) gets set to the end of the TextArea. &quot;did you just say Minkey?, yes that's what I said.&quot;

MrGreed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top