I am just starting out. I know HTML, CSS, JavaScript, and just now learning jQuery. I have 3 input boxes & a button. I do not want the button to be clickable if any of the input boxes are empty. Here is how my code stands now...
let garage = [];
const maxCars = 100;
class Car{
constructor(year, make, model){
this.year = year;
this.make = make;
this.model = model;
}
}
$(document).ready(function() {
$('#addCarButton').on('click', function() {
let newCar = new Car($('#yearInput').val(), $('#makeInput').val(), $('#modelInput').val() );
if (garage.length < maxCars){
garage.push(newCar);
} else {
console.log('Sorry garage is full');
return false;
}
updateGarage();
$('#yearInput').val('');
$('#makeInput').val('');
$('#modelInput').val('');
});
});
function newCar(year, make, model){
console.log('in newCar:', year, make, model);
garage.push(new Car(year, make, model));
return true;
}
function updateGarage() {
let outputElement = $('#garageList');
outputElement.empty();
for (let car of garage) {
outputElement.append('<li>' + Number(car.year) + ' ' + car.make + ' ' + car.model + '</li>');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Garage</title>
</head>
<body>
<h1>Garage</h1>
<div id="garageDiv"></div>
<div id="inputDiv">
<input type="number" placeholder="year" id="yearInput" >
<input type="text" placeholder="make" id="makeInput" >
<input type="text" placeholder="model" id="modelInput" >
<button type="button" id="addCarButton">Add Car</button>
</div>
<ul id="garageList">
</ul>
<script src="scripts/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="scripts/scrap.js" charset="utf-8"></script>
</body>
</html>
I am thinking that the solution will be something like this...
$(document).ready(function() {
$('#addCarButton').prop('disabled', true);
if ($('#modelInput').val().length != 0) {
$('#addCarButton').prop('disabled', false);}
$('#addCarButton').on('click', function() {
I believe that the disable/enable works, but I just don't know what conditional to use. The one I have is just testing 1 input, but I want it so that the button is only enabled when there is content in each input.
When I just run what I have here I, the button is disabled no matter what. I played around and can get it to be enabled if some random condition is true.
I also feel like I need to have a way to run the conditional multiple times to check, but I'm not sure how.