SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Nov 2001
    Location
    Dublin, Ireland
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Clicking a link puts text in a textarea (much like vB code), please help.

    Hi, I am building a web based site administration tool and I would like to implement a system whereby when a site administrator is adding a news item or whatever they can click on a text or button link (say, "Bold") and it will automatically insert the correct tags (i.e. "<b></b>"). Almost exactly like vBulletins code imput buttons when you are composing a new topic or reply. But with vBulletins version you are presented with a textbox you need to type in which then puts the code and your text in the form, I want my version to just put the code in. I can't really explain it but I hope you get it. I don't think this should be too long of a script, one or two functions at most but I really have no idea how to write it, can any SitePointers help me?

  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, but keep in mind that if you let people insert html into your textbox, without doing an htmlEncode() or something like that before you send it to the database, they can insert malicious code(redirects, popups, images etc...) that will fire each time you go to render the info. conversly, if you implement sometype of htmlEncode() function than the following concept won't work.

    anyway, you could do something like this:


    <script language="javascript" type="text/javascript">
    function insertCode(sInput) {
    frmYouForm.taYourTextArea.value = frmYouForm.taYourTextArea.value + sInput;
    }
    </script>


    <input type="button" value="BOLD" id="btnBold" onClick="insertCode('<b></b>')">

    <input type="button" value="Underline" id="btnUnderline" onClick="insertCode('<u></u>')">

  3. #3
    SitePoint Addict
    Join Date
    Nov 2001
    Location
    Dublin, Ireland
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't worry about security, I know what I am doing. I just don't have much JavaScript experience with forms.

    Anyway, I am trying this code:
    Code:
    <html>
    <head>
    <title>JavaScript Test</title>
    <script language="javascript" type="text/javascript"> 
    function insertCode(sInput) { 
    frmInputForm.taBody.value = frmInputForm.taBody.value + sInput; 
    } 
    </script> 
    </head>
    <body>
    <input type="button" value="BOLD" id="btnBold" onClick="insertCode('<b></b>')"> 
    <input type="button" value="Underline" id="btnUnderline" onClick="insertCode('<u></u>')">
    <hr />
    <form name="InputForm">
    <textarea name="Body"></textarea>
    </form>
    </body>
    </html>
    ... but when I click either of the buttons I get the following error ...

    'frmInputForm' is undefined.
    ... any ideas?

  4. #4
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for starters, put all of your elements inside the <form> tags, including the buttons.

    next, if you name your form "InputForm" and you name your textarea "Body", then your DOM needs to be:

    InputForm.Body.value = .....

    not:

    frmInputForm.taBody.value


    i preface my form element names so i know what kind of element it is but it is not required, just make sure the 2 match

  5. #5
    SitePoint Addict
    Join Date
    Nov 2001
    Location
    Dublin, Ireland
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, gottit. Thanks for all your help, if you ever need some PHP help ... look me up.

  6. #6
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right on, brotha!

  7. #7
    SitePoint Addict
    Join Date
    Nov 2001
    Location
    Dublin, Ireland
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lol!


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
  •