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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.