www.webdeveloper.com
Results 1 to 5 of 5

Thread: how to display the user's comment in the text as a html using javascript

  1. #1
    Join Date
    May 2014
    Posts
    2

    how to display the user's comment in the text as a html using javascript

    I am trying to create a comment page that displays a text box for the user to enter their comment and display it at a html paragraph. I have this part created but the issue I am having is that I'm trying have the first user's comment displayed as a paragraph and then the next user's comment to display below it. So essential, I looking to create multiple paragraph based on the user's comments in the text box. I can't use document.write because the paragraph is create in a separate page and I want everything displayed in one page. This is what I have so far. I can only use regular js, no jquery please

    </div>

    <div id="commentbackground">

    <p id="para"></p>



    <form>
    <textarea id="words" rows="30" cols="20">Enter comment</textarea>
    <input type="button" onclick="getwords()" value="Enter" />
    </form>

    <script type="text/javascript">
    function getwords(){
    text = words.value;
    document.getElementById("para").innerHTML = text
    document.getElementById("words").innerHTML = "enter"
    }
    </script>

    </div>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Where are the previous comments coming from
    and where are the new comments to be stored (and how)?


    Your code, slightly modified...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    
    <form>
    <textarea id="words" rows="10" cols="20">Enter comment</textarea>
    <input type="button" onclick="getwords()" value="Enter" /> <br>
    <p id="para"></p>
    </form>
    
    <script type="text/javascript">
    function getwords() {
      text = words.value;
      document.getElementById("para").innerHTML += '<p>'+text
      document.getElementById("words").value = "enter"
    }
    </script>
    
    </body>
    </html>
    Last edited by JMRKER; 05-08-2014 at 09:31 PM.

  3. #3
    Join Date
    May 2014
    Posts
    2
    The previous comment is coming from the first user. My goal is to have a user write a comment in the textbox, then click the onclick button, then have JS display its comment on the samepage. Then have another user comment and have it's comment display below the other user's comment. For example, this thread. I posted something and you just replied to my post.

  4. #4
    I don't think you're grasping what JavaScript is, or more specifically how it works on a page. JavaScript is a 'cleint side' technology (at least in this case), which means anything you do in it, until you send to the server, ONLY EXISTS for that one user on their browser. To do something like how a forums or a chat client works you need to send that form to a SERVER, for it to store it (either in a static file or a database) and disseminate it to all the other browsers, either as pull -- which is where people reload or revisit the page or the script polls the server periodically -- or by push, a far more complex process of keeping a connection open to the server that unless you're rolling in the cash, can get very expensive on hosting very quickly... much less "push" API's are still very new, inconsistent cross browser, and often require a plug-in type fallback. (flash or silverlight for example)

    Really to do what you are asking you should first make it work WITHOUT JavaScript by using a server-side language like PHP, ASP, Perl, Ruby, whatever. You're going to need to have server processing working anyways, so you might as well follow the unwritten rule of JavaScript:

    "If you can't make it work without scripting first, you likely have no business adding scripting to it!"

    You'll need to store each post either in a static file (which is a security risk and gets very poor performing very quickly) or a database -- which means learning a technology like mySQL, postGreSQL, MSSQL, SQLite, etc, etc...

    THEN if you want scripting, to do what you are asking in terms of posting it so other users on their own computers can see it instead of letting the already working form submit normally, you'd trap it's "onsubmit", cancel it, and use AJAX to send the new value instead; which is to say you use a XMLHttpRequest object to send the data, and I'd probably have it check for other updates while at it.

    Basically, JS alone isn't gonna do what you are asking, as you'll need a server side language, database language, and a proper normal form submit working first before you even THINK about getting client side scripting involved.

    That is, assuming I understood your last post...

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Quote Originally Posted by herrera128 View Post
    The previous comment is coming from the first user. My goal is to have a user write a comment in the textbox, then click the onclick button, then have JS display its comment on the samepage. Then have another user comment and have it's comment display below the other user's comment. For example, this thread. I posted something and you just replied to my post.
    Ditto post #4. Not gonna happen with JS only.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles