Hi, SO people. Can you help me with my homework? I am a beginner in jQuery and need to solve a "puzzle" that my teacher proposed to the class and allowed us to search here on the site.
Well, we have a list where some car models and their colors are listed, where such information is stored within their respective global data-car and data-color attributes. When the user selects a car from the list, it is directed to a form, which when sent, displays a message saying which car was chosen and its color. So far so good!
The problem is that when the user submits the form and returns (via the Close button) to choose a new car in the list and sends a new form, the information displayed on the screen is always that of the first car chosen. >
The correct thing would be that when a new car model was chosen, the car information on the form would be overwritten and the information on the screen would be that of the new car chosen rather than the first one.
We need to indicate a solution and point out why this is happening. Analyzing the code, we believe that the problem is solved by changing the first lines of the 3 functions in jQuery that start in different ways:
Function 1: Choose car
$(function() {
Function 2: Closes the form and returns to the list of cars
$(document).on('click', '.close', function(){
Function 3: Displays the car message which car was chosen
$(document).ready(function () {
Whoever can help us and make this system work properly, displaying the message properly, will be of our enormous prestige and consideration.
Thank you!
$(function() {
$(".cars").on('click', function() { //Função para escolher o carro
var car = $(this).data('car'); //Escolhe o modelo do carro
var color = $(this).data('color'); //Escolhe a cor do carro
$('#carlist').addClass('d-none'); //Fecha a lista de carros
$('#carinfo').removeClass('d-none'); //Abre o formulário
$('#mycar').attr('data-car', car); //Altera o modelo do carro no formulário
$('#mycar').attr('data-color', color); //Altera a cor do carro no formulário
});
});
$(document).on('click', '.close', function() { //Função para fechar o formulário
$('#carlist').removeClass('d-none'); //Aparece a lista de carros
$('#carinfo').addClass('d-none'); //Some o formulário
$('#mycar').attr('data-car', '0'); //Zera novamente o valor "car" do formulário
$('#mycar').attr('data-color', '0'); //Zera novamente o valor "color" do formulário
});
$(document).ready(function() {
$("#form").on('submit', function(e) {
e.preventDefault(); //Evita submeter o formulário via GET
var car = $('#mycar').data('car'); //Carrega o carro escolhido
var color = $('#mycar').data('color'); //Carrega a cor escolhida
alert(car + " - " + color); //Resultado
});
});
.cars {
cursor: pointer;
}
.cars:hover {
color: #FF0000;
}
.d-none {
display: none;
}
.close {
cursor: pointer;
}
.close:hover {
color: #FF0000;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script><divid="carlist">
<div class="cars" data-car="BMW" data-color="black">BMW (Black)</div>
<div class="cars" data-car="Audi" data-color="white">Audi (White)</div>
<div class="cars" data-car="Ferrari" data-color="red">Ferrari (Red)</div>
<div class="cars" data-car="Lamborghini" data-color="yellow">Lamborghini (Yellow)</div>
</div>
<div id="carinfo" class="d-none">
<span class="close">Close</span>
<form id="form">
<input type="text" id="mycar" data-car="0" data-color="0" autocomplete="off">
<input type="submit" value="Send">
</form>
</div>