Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Generate Code in Textarea

  1. #1
    Join Date
    Sep 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Generate Code in Textarea

    Hi,

    I want to create a form which has some checkboxes and text field. Using these checkboxes I want to generate some code that would display in the Textarea.

    Something similar to how Google Adsense generates the code.

    I know how to do it via PHP. But, looking to do the same via JS. Any ideas?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    This won't be too helpful as I'm not expert with javascript, but basically.... here's an example:

    Code:
    <input type="checkbox" onClick="if (this.value != checked) {textarea.value = 'this is some code'} else {textarea.value = ''}">
    <textarea id="textarea"></textarea>
    <br><br><br>
    <input type="checkbox" onClick="if (this.value != checked) {textbox.value = 'this is some code'} else {textbox.value = ''}">
    <input type="text" id="textbox">
    Now, it obviously gets more complex if you want it to do something like add code to the middle of what's already there, or change what's already there in some other way, etc. It's possible, just more work. If you get more complex than what's above, I'd recommend making a function and referring to it from the input checkbox...
    <input type="checkbox" onClick="dostuff()">
    (and define dostuff() as a function in the head section, between <script> tags)


    Also, I'll note that I was a bit confused when writing this because I needed to use the id attribute on the fields, rather than name. Not sure why this is. (I'm using safari, so maybe that's part of it).

    Note: It's "!= checked" because when you're clicking, it's the opposite of what it changes to, so the text should match the opposite of what you clicked.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    minor fix to drj33's code:

    Code:
    <input type="checkbox" onClick="if (this.checked != true) {textarea.value = 'this is some code'} else {textarea.value = ''}">
    <textarea id="textarea"></textarea>
    <br><br><br>
    <input type="checkbox" onClick="if (this.checked != true) {textbox.value = 'this is some code'} else {textbox.value = ''}">
    <input type="text" id="textbox">

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Is that just the "proper" way, or does it make it more compatible?
    I just guessed and got it to work... I'm sure that's better... just wondering...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    drj33: I know that your version works but the difference between your version and my version is that in your version if the user checks the checkbox, it inserts the data into the textarea/textbox but when the user unchecks it, the data inserted will still be there. So, it depends on the OP how he wants it to be.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, well all you need to do is remove the else statement from mine.
    I wasn't sure either.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Sep 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks djr33 and shachi.

    I'm looking more in the likes of adding stuff inbetween.

    Do you have Google Adsense? I want it similar to how they generate the code.

    The text area will be a result of combinations of textboxes and checkboxes.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It would be easy enough to do this:

    textarea.value = "<html>"+textarea.value+"</html>"
    However, to take those back out or to specify a particular location for somethign would involve analyzing the exact characters and would get compelx quickly.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In both the examples above, the variables "textbox" and "textarea" are undefined.
    Code:
    <input type="checkbox" onclick="
      var textarea = document.getElementById("textarea");
      if (!this.checked)
        textarea.value = 'this is some code';
      else
        textarea.value = '';
    ">
    <textarea id="textarea"></textarea>
    <input type="checkbox" onclick="
      var textbox = document.getElementById("textbox");
      if (!this.checked)
        textbox.value = 'this is some code';
      else 
        textbox.value = '';
    ">
    <input type="text" id="textbox">
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey: I know that but it doesn't really matter does it?? I was too lazy to standardize it(and make it unobtrusive).

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
  •