[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

onFocus? indented when clicked on bold text in textarea, not when text is not bold

Hi, me again!
I have some code which makes highlighted text in a rich text box bold or unbold.
At the moment I have an image which onmouseover is indented and onmouseout is normal.
What I want is the code to see where the cursor is in the textbox (what place in the text it is clicked) and see whether it is bold or not. if it is then I want the image sbold and if it isn't then I want the image bold to be shown.

I believe it may be something to do with onfocus but my attempts have not worked.

Can anyone point me in the right direction, I'v been looking at other code but I can't distiguish which bit of code does this.

The code I already have is as follows:

<html>
<head>
<script>
function FormatText(command, option) {
            document.getElementById("edit").contentWindow.focus();
            document.getElementById("edit").contentWindow.document.execCommand(command, false, option);
            document.getElementById("edit").contentWindow.focus();
}
</script>
</head>
<body scroll=no onLoad='frames.edit.document.designMode = "On"'; rightmargin="0" bottommargin="0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<a href="javascript:" onClick="FormatText('bold','')" onmouseover="bold.src='images/buttons/sbold.gif'" onmouseout="bold.src='images/buttons/bold.gif'"><img name="bold" border=0 src="images/buttons/bold.gif">&nbsp;Make text bold</a>

<iframe id="edit" width="100%" height="57%"></iframe>
</body>
</html>

Thanks
0
wjdashwood
Asked:
wjdashwood
  • 2
1 Solution
 
NetGrooveCommented:
Here again a small example:

<html>
<head>
<script>
function FormatText(command, option) {
  document.getElementById("edit").focus();
  document.execCommand(command);
  document.getElementById("edit").focus();
}


// markSelectedElement(): called by onClick and onKeyup events
// on the contectEditable area
function markSelectedElement() {
   var r = document.selection.createRange();
}


// reset(): called from all over the place to make the toolbar
// represent the current text. If el specified, it was called from
// hover(off)
function reset(el)
{
   if (!el || el == btnBold)         setState(btnBold, document.queryCommandValue('Bold'), 'images/buttons/bold.gif', 'images/buttons/sbold.gif');
}

// setState(): called from reset() to make a button represent the state
// of the current text.  Pressed is on, unpressed is off.
function setState(el, on, onGif, offGif)
{
      if (on) {
         el.src = onGif;
      } else {
         el.src = offGif;
      }
}

</script>
</head>
<body scroll="no">

<img name="bold" id="btnBold" border=0 src="images/buttons/bold.gif" onClick="FormatText('bold')">
<table id="editor" unselectable="on" height="100%" cellspacing="0" cellpadding="0" width="100%" bgcolor="buttonface" border="1"><tr><td>
<div id="edit" onkeyup="reset(); markSelectedElement();" contenteditable="true" style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 100%" onclick="reset(); markSelectedElement()"  onmouseup="markSelectedElement();" height="100%" width="100%">
</td></tr></table>
</div>
</html>

0
 
NetGrooveCommented:
Parts of this example are from this version:
http://richtext.cs.ramesys.com/rt-0.3/help/samples/test_embedded.asp


0
 
wjdashwoodAuthor Commented:
thanks, that seems to do what I wanted. A little more complicated than I hoped it would be but nevermind. I'll just have to work a little harder to get my head around it.

Thank you.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now