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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formatting text in a textarea

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am trying to get a button that will bold text in a textarea when it gets clicked. My code snippet is below, When I hit the button I get an error, "Object required".
My guess is that it is looking it cant find the textarea called "content". Any suggestions please!!


<td width=&quot;100%&quot; valign=&quot;top&quot;>
<p><font color=&quot;#000000&quot; size=&quot;2&quot; face=&quot;arial&quot;>&nbsp;
<textarea name=&quot;content&quot; cols=&quot;47&quot; rows=&quot;4&quot;>
</textarea>
<br>

<!--- Bolding Text Script--->
<script language=&quot;VBScript&quot;>
public sub cmdmakebold_onClick()

content.text.bold = true

End Sub
</script>
 
Hmm...

A couple of observations:

Event-handler script really should be declared in the <HEAD></HEAD> portion of the page rather than the body. Few browsers require this for the approach you have used, and as far as I know any of them that support VBScript and not just Javascript are somewhat forgiving of this. Some types of event-handling script just won't work declared at the end or in the middle of the <BODY> though. You really should strive to declare all script on a page within a single <SCRIPT> block, and that should be in the <HEAD>. Sure, there are exceptions but these are few. It is considered good practice to avoid intermixing script and HTML within the page whenever possible. There is a lot of hacked up ASP out there that is going to need a complete rewrite for .Net someday because the author is still writing stuff according to &quot;standards&quot; from 1997 when people didn't really understand ASP yet.

Your TEXTAREA has a name but doesn't have an id. The name is really only meant to give names to form elements for submission to the host, it wasn't intended to assign an element a scriptable identity. This is what id is for. People screwed this up so royally in early efforts at DHTML that many browsers will be forgiving here too and &quot;assume an id value equal to the name, but in some cases this can't happen due to ambiguity. Always declare both name and id and unless you have a very good reason always make them the same. It may seem like a pain, but it will keep you out of trouble. Besides, this is why tools like InterDev came out in the mid 90s - to automate much of this stuff.

DHTML is not VB. As a result, you will find that the DHTML object model is quite different from VB's in many regards. And wishing seldom makes things so in any programming environment!

Your example code assumes that a TEXTAREA object has a boolean property called &quot;bold.&quot; Here is what you need:
Code:
Public Sub cmdmakebold_onClick()
  content.style.fontWeight=&quot;bold&quot;
End Sub
To be a successful DHTML developer you need to study the browser document object model and CSS.

I find darned few good introductions are available, mostly because it is hard to get into much detail without things bogging down in the differences between the ways different browsers handle the same things. Since you seem to have chosen VBScript as your DHTML scripting language, you have narrowed your target browsers to Internet Explorer 4+ (older versions of IE simply being ridiculous to contemplate at this late date).

That's good news, because there is excellent reference information available, if not a lot of beginner tutorials! Run, don't walk, to
Be sure to click on Web Development in the navigation tree at the left. There is a wealth of good information here.
 
Umm..
I thought that was going to work but, but even after I gave it an id=&quot;content&quot; and changed the script code it still doesn't want to work.
 
Gosh, I am mystified. I tried this:
Code:
<HTML>
<HEAD>
<TITLE></TITLE>
<!--- Bolding Text Script--->
<SCRIPT language=&quot;VBScript&quot;>
Public Sub cmdmakebold_onClick()
	content.style.fontWeight = &quot;bold&quot;
End Sub
</SCRIPT>
</HEAD>
<BODY>
<TEXTAREA name=content rows=4 cols=47>
Hello World!
</TEXTAREA>
<BR>
<INPUT id=cmdmakebold name=cmdmakebold type=button value=Bold>
</BODY>
</HTML>
... and it works fine for me!

The Public keyword isn't needed, I was amazed to see that it was even accepted, but it works. My guess is Public relates to a technology called scriptlets that you don't need here.

But the code listed above works as is. Try pasting it into a new file by itself to make sure it works for you, then treat it as a starting point to figure out what isn't right in your real web page.

Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top