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
    Jul 2011
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts

    assign content of textarea to a div as in twitter

    i want to assign content of textarea to a div .. like in twitter when you type something and press "Tweet" a div is created and has the content of textarea..
    how can i implement it , i know something about getElementById() but not sure how it can help me here

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Example

    HTML
    Code:
    <textarea id="mytext"></textarea>
    <div id="container">
    </div>
    <button id="tweetit" onclick="tweetit();">Tweet</button>
    Javascript
    Code:
    function tweetit() {
       var newDIV = document.createElement('div');
       newDIV.className = "whatEverYouNeedForStyling";
       newDIV.innerHTML = document.getElementById('mytext').value; // put textarea value into new DIV
       document.getElementById('container').appendChild(newDIV); // append new DIV to container
    }

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Example

    HTML
    Code:
    <textarea id="mytext"></textarea>
    <div id="container">
    </div>
    <button id="tweetit" onclick="tweetit();">Tweet</button>
    Javascript
    Code:
    function tweetit() {
       var newDIV = document.createElement('div');
       newDIV.className = "whatEverYouNeedForStyling";
       newDIV.innerHTML = document.getElementById('mytext').value; // put textarea value into new DIV
       document.getElementById('container').appendChild(newDIV); // append new DIV to container
    }
    can u explain the javascript part plz , what is 'div' , in
    Code:
    document.createElement('div');
    is this fixed tagname of <div> or just a name
    Last edited by learner guy; 01-23-2012 at 12:59 PM.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    I already explained the last two lines using comments in the code

    The first two lines create a new DIV element "on the fly" and assign a class to it


  •  

    Tags for this Thread

    Posting Permissions

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