I have a bootstrap "form-group" that contains textarea and button form controls, I want to append the text which written inside the textarea to a list-group when button pressed
This is my page whith Jquery trial code :
$(document).ready(function() {
$('button:last').click(function() {
comment = $('textarea').val();
$('.list-group').append("<li class='list-group-item'>+comment+</li>");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
<div class="container">
<ul class="list-group">
</ul>
</div>
<div class="container" style="margin-top: 60px">
<form class="well col-xs-6 col-xs-offset-3">
<div class="form-group">
<label id="commentTitle" for="comment">Comment:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<button class="btn btn-primary ">submit</button>
</form>
</div>
you need to change
$('.list-group').append("<li class='list-group-item'>+comment+</li>");
into this $('.list-group').append("<li class='list-group-item'>"+comment+" </li>");
(careless mistake)
and add type="button"
$(document).ready(function(){
$('button:last').click(function() {
comment = $('textarea').val();
$('.list-group').append("<li class='list-group-item'>"+comment+"</li>");
});
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
</head>
<body>
<div class= "container">
<ul class="list-group">
</ul>
<div class="container" style="margin-top: 60px">
<form class="well col-xs-6 col-xs-offset-3">
<div class="form-group">
<label id="commentTitle" for="comment">Comment:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<button type="button" class="btn btn-primary ">submit</button>
</form>
</div>
</body>
</html>
Your are missing double quote (") check below code.
$(document).ready(function() {
$('button:last').click(function(event) {
event.preventDefault();
comment = $('textarea').val();
$('textarea').val('');
$('.list-group').append("<li class='list-group-item'>" + comment + "</li>");
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
<div class="container">
<ul class="list-group">
</ul>
</div>
<div class="container" style="margin-top: 60px">
<form class="well col-xs-6 col-xs-offset-3">
<div class="form-group">
<label id="commentTitle" for="comment">Comment:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<button class="btn btn-primary ">submit</button>
</form>
</div>
And check this link JSFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With