Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Removing specific tags in textarea

    Hi All,

    In 2006 Kov posted this code in reply to a question about removing tags from code appearing in a textarea.

    The code is fine for removing the noted tags but removes all of the say bold tags if more than one set is in the text area being edited. I was wondering if it can be modified to remove only tags where the text has been removed from between a set of tags but leave alone any other tags say the bold which have text within the tag.

    I hope that is clear enough. The example of what is needed is also written into the code to help.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/JavaScript">
    function removeTags(tag){
    var obj=document.getElementsByTagName('textarea')[0];
    var re= new RegExp('<'+tag+'[^><]*>|<.'+tag+'[^><]*>','g')
    obj.value=obj.value.replace(re,'');
    }
    </script>
    
    </head>
    
    <body>
    <textarea rows="6" value=""><b>boldhereg</b> this next tag being an empty tag to be the only one removed <b></b> <font>fonthere</font> <i>italichere</i></textarea>
    <br>
    <input type="button" value="Remove Bold" onclick="removeTags('b')">
    <br>
    <input type="button" value="Remove Font" onclick="removeTags('font')">
    <br>
    <input type="button" value="Remove Italic" onclick="removeTags('i')">
    </body>
    </html>
    Martin.
    Last edited by SpidersWebHelp; 04-28-2013 at 04:55 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Is this what you mean?

    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/JavaScript">
    function removeTags(tag){
    var obj=document.getElementsByTagName('textarea')[0];
    if (tag == "b") {
    var re = new RegExp('<b> *</b>','g');
    }
    else {
    var re= new RegExp('<'+tag+'[^><].*>|<.'+tag+'[^><]*>','g');
    }
    obj.value=obj.value.replace(re,'');
    }
    </script>
    
    </head>
    
    <body>
    <textarea rows="8" value=""><b>boldhereg</b> this next tag being an empty tag to be the only one removed <b> </b> <font>fonthere</font> <i>italichere</i></textarea>
    <br>
    <input type="button" value="Remove Bold" onclick="removeTags('b')">
    <br>
    <input type="button" value="Remove Font" onclick="removeTags('font')">
    <br>
    <input type="button" value="Remove Italic" onclick="removeTags('i')">
    </body>
    </html>

    BTW, when posting here please help us to help you by making it easier to copy, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.




    A man explained inflation to his wife thus: 'When we married, you measured 36-24-36.
    Now you're 42-42-42. There's more of you, but you are not worth as much.
    Last edited by Philip M; 04-28-2013 at 03:28 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you, that is what I meant.

    I have an additional question related to the font tag. Is there a wild card of some sort which would take care of any additional character which could appear in a font tag such as a color which maybe be in letter form such as red or blue or mixed as #0099FF.

    Examlpe:

    <font color="red"> or <font style="font-size:12px"> or <font face="Microsoft Sans Serif">


    Martin.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Code:
    else if (tag == "font") {
    var re = new RegExp('<font.*>.*</font>','g');
    }
    But the <font> tag is obsolete and not supported in HTML5, and is deprecated in HTML 4.01. These days you should use css for styling.
    For example, you can use the CSS "font-weight" property to set bold text.
    Last edited by Philip M; 04-28-2013 at 06:36 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •