Jump to content

[SOLVED] getting selected text


mike12255

Recommended Posts

so im trying to get the text the user selects and add bold tags around them problem is my button dosnt seem to work, can anyone figure out why?

 

 

<html>
<head>


<script language="javascript" type="text/javascript">

function ShowSelection()
{
  var textComponent = document.getElementById('outputtext');
  var selectedText;
  // IE version
  if (document.selection != undefined)
  {
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
document.myform.outputtext.value += "[b]" + selectedText + "[/b]";
  }
  // Mozilla version
  else if (textComponent.selectionStart != undefined)
  {
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos)
document.myform.outputtext.value += "[b]" + selectedText + "[/b]";
  }
// alert("You selected: " + selectedText);
}
</script>

<form name="myform">


<input type="button" value="Option One" onClick="addtext1();">

<input type="button" value="Bold" onClick="ShowSelection();">



<textarea name="outputtext" cols="30" rows="25">Dear </textarea>



</form>



<!--next-->
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/173047-solved-getting-selected-text/
Share on other sites

Hi, not sure that it is the only problem with the script, but you are defining the textComponent by the id, however this is not defined.

var textComponent = document.getElementById('outputtext');

For your textarea you have nly goiven it a name, if you want to use getElementById() you need to define the id attribute as well.

<textarea id="outputtext" name="outputtext" cols="30" rows="25">Dear </textarea>

 

Hope this helps you on your way :)

Chris

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.