Question

Here is the fiddle.

I am trying to make a comment system by getting the value of <textarea id='type'></textarea> and prepend it as a div. The JS is currently:

$(document).ready(function(){
    $('#b').click(function(){
        var v = $('#type').val();
        $('div').prepend(v);
    });
});

<h1>Comments</h1>

<textarea id='type'></textarea>
<br />
<br />
<button id='b' onclick='comment()'>Submit</button>

But for some reason this code isn't showing the prepended var.

Edit

I have updated to have div but had already tried it. It didn't show up.

Was it helpful?

Solution

DEMO HERE

I added a div with id='div1' because you try to prepend to undefined div.

Also styled your div with color:white so that you can see the text:

 <h1>Comments</h1>
 <textarea id='type'></textarea>
 <br />
 <br />
 <button id='b' >Submit</button>
 <div id='div1'></div>

And css :

#div1 {
   color:white;
 }

jQuery code :

$(document).ready(function () {
    $('#b').click(function(){
        var v = $('#type').val();
        $("#div1").prepend(v);
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top