<div >
<textarea ></textarea>
<input type="submit" value="Send" />
</div>
How to retrieve data in textarea field after clicking on submit button. Using jquery, ajax?
CodePudding user response:
This code should work:
function getdata(Event){
Event.preventDefault()
let chatInput = $('#chat-input').val();
$('#result').text(chatInput)
}
$('#message-send').click(getdata);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<form > <!-- replace div with form -->
<textarea id="chat-input"></textarea>
<input type="submit" value="Send" id="message-send" />
</form>
<p> for the example we will place the text on the div bellow. but use the data however you like</p>
<div id='result'>
</div>
Note that I changed the div tag into a form tag, and added some IDs.
CodePudding user response:
You can find here a solution using jQuery. The output is currently store in a div with id="out", you can customize the code.
document.forms["chat-input-holder"].addEventListener('submit', e => {
e.preventDefault();
let text = $(".chat-input").val();
$("#out").html(text);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="chat-input-holder">
<textarea ></textarea>
<input type="submit" value="Send" />
</form>
<div id="out"></div>
