SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trouble Populating Anchor Text in TextArea

    I am creating a simple, small CMS module for a client. I created a little form and when they click Submit, it goes straight out into an include (.inc) file, which is connected to the web page to be displayed.

    One of the buttons I want to insert an example URL so they don't have to mess around with tags. I need it to exactly insert this into the textarea:

    <a href="http://www.example.com"> Link Text Here </a>

    Since the data from the textarea gets passed into an html page, I need to be able to pass the anchor tags too. The trouble is, the string (highlighted below) wants to read it as an actual URL and not just spit it out as plain old text. I can make it populate plain old text - that works just fine. But instead it acts like I am trying to put actual html in there... Is there some special trick I am missing?


    <form method="post" action="<?=$_SERVER['PHP_SELF']?>">

    <script type= "text/javascript">

    // myField accepts an object reference, myValue accepts the text string to insert at the cursor

    function insertatcursor(myField, myValue) {
    if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    }
    else if (myField.selectionStart == 0 || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0,startPos) + myValue + myField.value.substring(endPos, myField.value.length);
    }
    else {
    myField.value += myValue;
    }
    }
    </script>


    <table width="50%" border="0" align="center" cellpadding="1" cellspacing="1" valign="top">
    <tr>
    <td width="2%"></td>
    <td align="center" valign="top">
    <textarea id="mytext" name="savecontent" cols="50" rows="10" style="text-align: left; padding: 0px; overflow: auto; border: 3px groove; font-size: 12px"><?=$loadcontent?></textarea></td>
    <td align="left" valign="top" width="60%">
    <input name="url" type="button" value="insert url" onclick="insertatcursor(document.getElementById('mytext'), '<a href="http://www.example.com">Link Text</a>'); return false;">
    </td>
    </tr>
    <tr>
    <td></td>
    <td align="center" valign="top"><input type="submit" name="save_file" value="SAVE"> </td>
    <td align="left" valign="top">&nbsp;</td>
    </tr>
    </table>

    <br />
    </form>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    How do things go when you escape the intended text content?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know what you're asking exactly?

    but I figured out the answer and I thought that I'd share;

    <input name="url" type="button" value="insert url" onclick="insertatcursor(document.getElementById('mytext'), '\x3ca href=\x22http://www.example.com\x22 target=\x22_blank\x22>Link Text\x3c/a\x3e'); return false;">

    Awesome!!


Bookmarks

Posting Permissions

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