Is it possible to rewrite the following code into pure javascript? I don’t want to use jquery.
$(function () {
var aggregateInput = function () {
var value = ('Website Url: ') + jQuery('input[name="url"]').val() + ' \n \n' +
('Subject: ') + jQuery('input[name="subject"] \n').val() + ' \n \n' +
('Brief Message: ') + jQuery('textarea[name="customer service"] \n \n').val().replace(/\n/g, '<br/>');
jQuery('textarea[name="email-message"]').val(value);
}
jQuery('.contact-style').on('keyup', aggregateInput);
});
$(document).ready(function () {
$('.contact-form-button-submit').attr('disabled', true);
$('.customform-message').keyup(function () {
if ($(this).val().length != 0) {
$('.contact-form-button-submit').attr('disabled', false);
} else {
$('.contact-form-button-submit').attr('disabled', true);
}
})
});
>Solution :
document.addEventListener('DOMContentLoaded', function () {
var aggregateInput = function () {
var value = ('Website Url: ') + document.querySelector('input[name="url"]').value + ' \n \n' +
('Subject: ') + document.querySelector('input[name="subject"] \n').value + ' \n \n' +
('Brief Message: ') + document.querySelector('textarea[name="customer service"] \n \n').value.replace(/\n/g, '<br/>');
document.querySelector('textarea[name="email-message"]').value = value;
}
document.querySelector('.contact-style').addEventListener('keyup', aggregateInput);
});
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('.contact-form-button-submit').setAttribute('disabled', true);
document.querySelector('.customform-message').addEventListener('keyup', function () {
if (this.value.length != 0) {
document.querySelector('.contact-form-button-submit').setAttribute('disabled', false);
} else {
document.querySelector('.contact-form-button-submit').setAttribute('disabled', true);
}
});
});