Reputation: 121
I want to make a textarea to show its content to a div by a clicking a button. I wrote a simple code that it sees for short time in div but it disappears in div and textarea quickly.
how can I correct it, for now I’m only designing the page and I want to make style for answer so I need to see content in div without connect it to server.
<textarea class="inBox"></textarea><button class="btn">Send</button>
Code link: jsfiddle
Upvotes: 1
Views: 124
Reputation: 21
JQuery
$(document).ready(function(){
$('.btn').on('click',function(e){
e.preventDefault();
var inpx= $('.inBox').val();
$('#mainbox').append(inpx);
});
});
Upvotes: 2
Reputation: 113
Use following code
HTML
<form action="#">
<textarea class="inBox"></textarea><button class="btn">Send</button>
</form>
JQuery
$(document).ready(function(){
$('.btn').on('click',function(){
var inpx= $('.inBox').val();
$('#mainbox').text(inpx);
});
});
Upvotes: 2
Reputation: 3148
See the update fiddle:
added the following code in the click function:
e.preventDefault();
To prevent the page reload/load.
"https://jsfiddle.net/h84qu8x3/12/"
Upvotes: 3
Reputation: 2419
Add return false;
to the end of your method to prevent post action or change inputs type to button
.
Your problem is in reload form by submit button click.
Upvotes: 2
Reputation: 74420
By default, type of button
is submit
, so that submits the FORM. Set its type to button
:
<button type="button" class="btn">Send</button>
Upvotes: 1