Click to See Complete Forum and Search --> : how to make text in textarea bold


dany999
07-08-2003, 10:12 AM
Hello all,
I want to make the content of the text area to be BOLD after mouse click event. I am able to call a java script function on mouse click .where I am having the logic for changing the plain text to bold.

This is the code I have written inside the function

function chanceText(){
var val =document.forms[0].text.value;
// 'text' is the text area name
document.forms[0].text.value=val.bold();
}

problem with the code is I am seeing HTML block code inserted .(<B></B>

If i have “July” in the text area, after execution of the function i see <B>July</B> instead of July being in block letters.

Please correct me where I am going wrong. If this issues is already discussed in the forum. Please pass on the message number.

Thanks,

freefall
07-08-2003, 10:21 AM
<script type="text/javascript">
function changeText(){
document.forms[0].text.style.cssText = "font-weight: bold";
}
</script>
<form>
<textarea name="text" onclick="changeText()">Hello!</textarea>
</form>


This uses CSS, you were on the right track, but <b> is seen as part of the textarea's value and it doesn't perform any particular action.

- Ian

dany999
07-08-2003, 10:29 AM
Thanks Ian...

now my problem is with the selected text within a word ...

For instance in Dany999, i select Dany and on click of a Button(Bold) i want Dany to be BOLD . i used the following java script function....

function format_sel(v) {
var str = document.selection.createRange().text;
document.my_form.my_textarea.focus();
var sel = document.selection.createRange();
sel.text = "<" + v + ">" + str + "</" + v + ">" ;
return;
}

i see <b>Dany</b>999 instead of Dany999 where 'Dany' is displayed in Bold .is there a way i can apply Style only for a selected Text or anyother suggestion.


Thanks



My Source Code :
==============

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT>
function mouseover(el) {
el.className = "raised";
}

function mouseout(el) {
el.className = "button";
}

function mousedown(el) {
el.className = "pressed";
}

function mouseup(el) {
el.className = "raised";
}
function format_sel(v) {
var str = document.selection.createRange().text;
document.my_form.my_textarea.focus();
var sel = document.selection.createRange();
alert(sel.text);
sel.text = "<" + v + ">" + str + "</" + v + ">" ;
return;
}
function insert_link() {
var str = document.selection.createRange().text;
document.my_form.my_textarea.focus();
var my_link = prompt("Enter URL:","http://");
if (my_link != null) {
var sel = document.selection.createRange();
sel.text = "<a href=\"" + my_link + "\">" + str + "</a>";
}
return;
}

</SCRIPT>

<style type ="text/css" >
#toolbar {
margin: 0;
padding: 0;
width: 262px;
background: buttonface;
border-top: 1px solid buttonhighlight;
border-left: 1px solid buttonhighlight;
border-bottom: 1px solid buttonshadow;
border-right: 1px solid buttonshadow;
text-align:right;
}
H2 {color:red; font-size:33pt; font-style:italic;}

.my_textarea {
font-family:"Comic Sans MS";
font-size:12px;
background-color:pink;
color:green;
}

.button {
background: buttonface;
border: 1px solid buttonface;
margin: 1;
}

.raised {
border-top: 1px solid buttonhighlight;
border-left: 1px solid buttonhighlight;
border-bottom: 1px solid buttonshadow;
border-right: 1px solid buttonshadow;
background: buttonface;
margin: 1;
}

.pressed {
border-top: 1px solid buttonshadow;
border-left: 1px solid buttonshadow;
border-bottom: 1px solid buttonhighlight;
border-right: 1px solid buttonhighlight;
background: buttonface;
margin: 1;
}
</style>
</HEAD>

<BODY>
<H2> Test </H2>
<form name="my_form">

<div id="toolbar">
<img class="button"
onmouseover="mouseover(this);"
onmouseout="mouseout(this);"
onmousedown="mousedown(this);"
onmouseup="mouseup(this);"
onclick="format_sel('b');"
src="bold.gif"
width="16" height="16"
align="middle"
alt="click to make your selection bold">
<img class="button"
onmouseover="mouseover(this);"
onmouseout="mouseout(this);"
onmousedown="mousedown(this);"
onmouseup="mouseup(this);"
onclick="format_sel('i');"
src="italic.gif"
width="16" height="16"
align="middle"
alt="click to make your selection italic">
<img class="button"
onmouseover="mouseover(this);"
onmouseout="mouseout(this);"
onmousedown="mousedown(this);"
onmouseup="mouseup(this);"
onclick="insert_link();"
src="link.gif"
width="32" height="16"
align="middle"
alt="click to add a link">
</div>
<textarea cols="30" rows="6" id="text" type ="text/html"name="my_textarea" ></textarea>
</form>
</BODY>
</HTML>

freefall
07-08-2003, 11:52 AM
I'm inclined to say there's no way you can do exactly that, not with Javascript. What is this being used for? I'm sure there's probably some way or another to get the desired effect, but a textarea can only have one set font style so far as I know.

Sorry,
Ian

dany999
07-08-2003, 12:37 PM
Ian,

I am developing a HTML Textarea editor .smilar to yahoo/ hotmail email editor which has option for user to change the font, format text like bold , itlaics.

The point is user should not see any HTML Tag . he should feel as if formating a text in Microsoft Word.

I got many code sample ..but most of them where browser specific(IE and windows only).hence i went to handcoded with pure HTML and Javascript which will be cross compatable with all browser.

Hope i was clear .

please suggest if you have any idea on developing rich text editor which will be compatible on all browser (atleast IE and netscape)..please let me know.

Thanks,
dany

cacalex
07-08-2003, 01:04 PM
Why to put it in a textarea ????

If you use a DIV, or SPAN, you might be able to do it ...

cacalex
07-08-2003, 01:11 PM
Forget my last post...

Here's the hotmail way to do it in a textarea...

<td colspan=2 align="center">
<div id="cdiv" style='position:relative; left:0px; top:0px; height:314px; width:603px;border:solid 1px #9C9C9C;'>
<textarea name="body" wrap="soft" style='visibility:visible; z-index:100; position:absolute; left:0px; top:0px; height=100%; width=100%'>Text???</textarea>
<IFRAME SRC="/cgi-bin/dasp/EN/rte.asp" id='richedit' style='visibility: hidden; position: absolute; left: 0px; top: 0px; height=100%; width=100%'></IFRAME>
</div>
</td>

dany999
07-08-2003, 02:13 PM
hi cacalex

thanks for the info.

I am working as per your suggestion. looks like i am progressing on the right direction. will post the result after i tested it.

~dany

cacalex
07-09-2003, 02:45 PM
Take a look at this :

http://home.att.net/~codeLibrary/HTML/html_edit.htm

They got an "html text editor", wich look like what you want...

Hop it will help you !

dany999
07-09-2003, 03:11 PM
hi cacalex ,

Truly appreciate you for providing the link.

The code they have discussed in the link will work only for IE. i like to have the application browser compatible. Thats the sole reason i want to use pure HTML and Java script.

infact i used <div contentEditable = true id= "text"></div>

the code works very well in IE .but in Netscape the content was not editable.

trying with different option .........

~dany

cacalex
07-09-2003, 03:21 PM
I'll stay in 'stand-by' !!!

mago
07-27-2004, 09:37 AM
Originally posted by dany999
hi cacalex ,

Truly appreciate you for providing the link.

The code they have discussed in the link will work only for IE. i like to have the application browser compatible. Thats the sole reason i want to use pure HTML and Java script.

infact i used <div contentEditable = true id= "text"></div>

the code works very well in IE .but in Netscape the content was not editable.

trying with different option .........

~dany


Hey Dany, wondering if you ever find any Netscape-compatible alternative?

Mago

BillyRay
07-27-2004, 09:41 AM
It's amazing what a Google search can throw up. I searched for:

cross browser wysiwyg html editor[code]

And the first thing that came up? "Cross-Browser Rich Text Editor": http://www.kevinroth.com/rte/demo.htm

If only more people searched for what they were after before giving up :rolleyes:

Dan

BillyRay
07-27-2004, 09:42 AM
OK - ignore the "[code]" bit in the last post. I definately did not search for that too ;)

Dan