I have a textarea like below, when the value is empty then the "convert" and "clear value" buttons are not active/disable, they will be active only if the textarea field has a value.
If the value is cleared manually it has worked properly, but the problem is if the "clear value" function works to empty the text area value the button is still active.
how to fix that when the textarea field is emptied by the "clear value" function, the two buttons become inactive. so they are only active when the textarea field contains a value.
let $btn = document.querySelector('#convert')
$btn.addEventListener('click', function() {
let inputArr = document.querySelector('#inputTextarea').value.split('\n');
let $result = document.querySelector('#outputTextarea')
let number = 1
let final = []
for (row of inputArr) {
let rowArr = row.split(' | ')
let first = rowArr.shift().split(':')
let nrString = `+No.` + number
let mail = `Email: ${first[0]}`
let token = `Token: ${first[1]}`
rowArr.unshift(nrString, mail, token);
rowArr = rowArr.join('\n')
final.push(rowArr)
number++
}
let finalText = final.join('\n\n')
$result.value = finalText
});
//button copy result
let buttonCopy = document.getElementById("copyResult"),
input = document.getElementById("outputTextarea");
buttonCopy.addEventListener("click", function(event) {
event.preventDefault();
input.select();
document.execCommand("copy");
//alert("Result copied");
});
//Clear input textarea value
const inputTextarea = document.getElementById("inputTextarea");
const btnClear = document.getElementById('clearValue');
btnClear.addEventListener('click', function handleClick() {
inputTextarea.value = '';
});
//disable action button if textarea empty
$(document).ready(function() {
$('.inputTextarea textarea').keyup(function() {
var empty = false;
$('.inputTextarea textarea').each(function() {
if ($(this).val().length == 0) {
empty = true;
}
});
if (empty) {
$('.actionTextarea button').attr('disabled', 'disabled');
} else {
$('.actionTextarea button').attr('disabled', false);
}
});
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
<div class="container m-4">
<div class="form-group inputTextarea">
<label for="inputTextarea">Paste bulk text bellow</label>
<textarea class="form-control" id="inputTextarea" rows="3"></textarea>
</div>
<!-- Button trigger modal -->
<div class="actionTextarea">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalTextarea" id="convert" disabled>
Convert
</button>
<button type="button" class="btn btn-secondary" id="clearValue" disabled>Clear value</button>
</div>
</div>
<!--Result area-->
<!-- Modal -->
<div class="modal fade" id="modalTextarea" tabindex="-1" role="dialog" aria-labelledby="modalTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Convert result</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<textarea class="form-control" id="outputTextarea" rows="8"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="copyResult">Copy Result</button>
</div>
</div>
</div>
</div>