this is what ive got, the item pushes to the array when typed in to the input boxes and the button is clicked to submit, i just cant seem to get it displayed on the html not sure where ive went wrong
JavaScript
x
38
38
1
$( document ).ready ( readyNow );
2
3
let garage = [];
4
5
function readyNow() {
6
console.log( 'JQ' );
7
$( '#addCarButton' ).on( 'click', addCar )
8
} //end readyNow
9
10
function addCar() {
11
console.log('in addCar');
12
//get unser inputs
13
//create new object
14
let newCars = {
15
year: $( '#yearInput' ).val(),
16
make: $( '#makeInput' ).val(),
17
model: $( '#modelInput' ).val()
18
}
19
//push the new car into the array
20
garage.push( newCars );
21
//empty inputs
22
$( '#yearInput' ).val( '' );
23
$( '#makeInput' ).val( '' );
24
$( '#modelInput' ).val( '' );
25
}
26
console.log(garage);
27
28
29
30
function displayGarage(){
31
console.log('in displayGarage');
32
33
$('#garageOut ').append
34
( '<li> Year: ' + newCars.year +
35
'Make: ' + newCars.make +
36
'Model: ' + newCars.model +'</li>');
37
}
38
JavaScript
1
27
27
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<meta charset="utf-8">
5
<script src="scripts/jQuery.js" charset="utf-8"></script>
6
<script src="scripts/client.js" charset="utf-8"></script>
7
<link rel="stylesheet" href="styles/style.css">
8
9
<title>Week 6 Tier 1 Assignment</title>
10
</head>
11
<body>
12
13
<h1><img src="images/logo.png" alt="noah's car garage"></h1>
14
15
<h2>Please Enter your Year, Make, and Model: <span id="garageList"></span></h2>
16
<input placeholder="Year" id="yearInput" />
17
<input placeholder="Make" id="makeInput" />
18
<input placeholder="Model" id="modelInput" />
19
<button id= "addCarButton">Add Car</button>
20
21
22
<h3>Garage:</h3>
23
<div id ="garageOut"></div>
24
</div>
25
</body>
26
</html>
27
please help, i can see that the array is outputted in the console when typed into the buttons but nothing shows up on my html, am i not sourcing it somehow? i have the id set to garageOut on a div on the html
Advertisement
Answer
- You are not calling
displayGarage
after adding a new car. - Simply calling it is not enough, you need to pass a new car to it
JavaScript
1
38
38
1
$(document).ready(readyNow);
2
3
let garage = [];
4
5
function readyNow() {
6
console.log('JQ');
7
$('#addCarButton').on('click', addCar)
8
} //end readyNow
9
10
function addCar() {
11
console.log('in addCar');
12
//get unser inputs
13
//create new object
14
let newCar = {
15
year: $('#yearInput').val(),
16
make: $('#makeInput').val(),
17
model: $('#modelInput').val()
18
}
19
//push the new car into the array
20
garage.push(newCar);
21
//empty inputs
22
$('#yearInput').val('');
23
$('#makeInput').val('');
24
$('#modelInput').val('');
25
26
displayGarage(newCar); // NEW
27
}
28
console.log(garage);
29
30
31
32
function displayGarage(newCar) { // NEW
33
console.log('in displayGarage');
34
35
$('#garageOut ').append('<li> Year: ' + newCar.year +
36
'Make: ' + newCar.make +
37
'Model: ' + newCar.model + '</li>');
38
}
JavaScript
1
30
30
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<meta charset="utf-8">
7
<script src="scripts/jQuery.js" charset="utf-8"></script>
8
<script src="scripts/client.js" charset="utf-8"></script>
9
<link rel="stylesheet" href="styles/style.css">
10
11
<title>Week 6 Tier 1 Assignment</title>
12
</head>
13
14
<body>
15
16
<h1><img src="images/logo.png" alt="noah's car garage"></h1>
17
18
<h2>Please Enter your Year, Make, and Model: <span id="garageList"></span></h2>
19
<input placeholder="Year" id="yearInput" />
20
<input placeholder="Make" id="makeInput" />
21
<input placeholder="Model" id="modelInput" />
22
<button id="addCarButton">Add Car</button>
23
24
25
<h3>Garage:</h3>
26
<div id="garageOut"></div>
27
</div>
28
</body>
29
30
</html>