0

Hi I'm working on a personal website and trying to make a comment box. its still very basic but I have a button that calls a function which stores the values of the name of the person and the comment itself, and then hopefully output it somewhere (I don't care where yet, just want to see an output)

this is a snippet of what I got so far but its not doing anything, and I don't know what I'm doing either ;) so please rescue me

               <section class="body_right_comment_input">
                    <p> Name: </p> 
                    <input type="text" id="name_input"/> </br>
                    <p> Comment:</p>
                    <textarea id="comment_input"></textarea>
                    <button onclick="myFunction()">Click me</button>

                </section>

        <script type="text/javascript">
            function myFunction() {
            var commentName = document.getElementById("name_input").value;
            var commentValue= document.getElementById("comment_input").value;
            document.write(commentName)
            document.write(commentValue)
            }
        </script>
6
  • Tip #1: Don't use document.write. Commented Jul 16, 2014 at 3:39
  • @j08691 how do I output the variables as a comment to the page? I dont know how to do that Commented Jul 16, 2014 at 3:40
  • It works fine for me: jsfiddle.net/AVzC3 Commented Jul 16, 2014 at 3:41
  • yes I had the closing bracket for the function right after the opening, so the function code was in thin air Commented Jul 16, 2014 at 3:42
  • I don't know the full requirements for what you are trying to do here. However, if you are trying to make a UI where you keep making comments and want to display them somewhere else on the page, I would consider learning a template based framework like knockoutJS. It is great for UI composition and building dynamic UIs knockoutjs.com Commented Jul 16, 2014 at 3:43

3 Answers 3

2

To output something to somewhere in your HTML, you can use innerHTML, which is like this:

document.getElementById('myAnchor').innerHTML="W3Schools";
Sign up to request clarification or add additional context in comments.

2 Comments

how do I specify a location, for example, within a certain section or certin div?
It's the same like how you get the value in your code. instead use getElementByTagName..
1

User innerHTML to output: http://jsfiddle.net/z4hjv/

<section class="body_right_comment_input">
    <p>Name:</p>
    <input type="text" id="name_input" />
    </br>
    <p>Comment:</p>
    <textarea id="comment_input"></textarea>
    <button onclick="myFunction()">Click me</button>
    <span id="comment_result"></span>
</section>

function myFunction() {
    var commentName = document.getElementById("name_input").value;
    var commentValue = document.getElementById("comment_input").value;
    document.getElementById("comment_result").innerHTML = commentName + commentValue;
}

1 Comment

ohhhh fascinating, I'm going to try this now! thanks!
1

FALSE ALARM! my closing bracket was in the wrong place for the function! However, feel free to answer and suggest a better way to do this or how I can output this into the html itself

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.