Solved

Insert bold tag in textarea bug

Posted on 2006-06-25
2
586 Views
Last Modified: 2008-03-04
Hi,

Using some code from another post, I am able to added <b> and </b> tags to a selected area. The problem is that when nothing is selected the <b> and </b> tags get added to the page (in front of the link or button used to create it).

Is there any way to make it work only when a bit of text is selected in the text area?

Code below.


Thanks in advance.

<html>
<head>
  <title>Untitled</title>
<script language="javascript">
<!--
function insertTags(event) {
  if (document.layers) {
    var range=document.getSelection();
     var wholeText=document.editor.notes.value;
  }
  if (document.all) {
    var range=document.selection.createRange();
     rangeText="<b>"+range.text+"</b>";
     range.text=rangeText;
  }
  document.editor.notes.focus();
 
}
//-->
</script>
</head>
<body>
<form name="editor">
  <textarea name="notes" rows="20" cols="80"></textarea><br>
  <a href="#" onClick="insertTags()">bold</a>
</form>
<br><br>
</body>
</html>
0
Comment
Question by:mrduckers
2 Comments
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16982143
<html>
<head>
  <title>Untitled</title>
<script language="javascript">
<!--
function insertTags(event) {
  /*if (document.layers) {
    var range=document.getSelection();
     var wholeText=document.editor.notes.value;
  }
  if (document.all) {
    var range=document.selection.createRange();
     rangeText="<b>"+range.text+"</b>";
     range.text=rangeText;
  }*/

  text = document.getElementById('notes').value;
  document.getElementById('notes').value = "</b>" + text + "</b>";
  document.editor.notes.focus();
 
}
//-->
</script>
</head>
<body>
<form name="editor">
  <textarea name="notes" id="notes" rows="20" cols="80"></textarea><br>
  <a href="#" onClick="insertTags()">bold</a>
</form>
<br><br>
</body>
</html>

Bonmat86.
0
 
LVL 30

Accepted Solution

by:
callrs earned 500 total points
ID: 16982372
Your code:
  if (document.all) {
    var range=document.selection.createRange();
       if(range.text.length==0) return;               // add this line
     rangeText="<b>"+range.text+"</b>";
     range.text=rangeText;
  }

Or Bonmat86's code:

  text = document.getElementById('notes').value;
          if(text.length==0) return;                           // add this line
  document.getElementById('notes').value = "</b>" + text + "</b>";
  document.editor.notes.focus();
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now