Solved

Insert bold tag in textarea bug

Posted on 2006-06-25
2
591 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 List with Ratings 6 37
Google Sheets Script 21 58
Opening PDF on button click and fill new document 2 21
Jquery get each value 2 17
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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

867 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

24 Experts available now in Live!

Get 1:1 Help Now