0
I need to check if a input type=radio was checked with jQuery to pass to the next step of the form. My function for this was already working, but I needed to transform the name of checkbox in array for it to store data of all options chosen.
Follows the code:
$("#button-food").click(function() {
var food = "";
$('input:checkbox[name=food[]]').each(function() {
if ($(this).is(':checked')) {
food = $(this).val();
console.log(food);
}
})
console.log(food);
if (food != "") {
$("#food-page").hide();
$("#drink-page").show();
} else {
alert("Você precisa escolher uma alternativa! Caso não coma nada, selecione a última opção.")
$("#button-food").blur();
}
})
The mistake only appeared after I changed the name of inputs for name="food[]", change that was necessary for the back-end of the site.
And the console error message was as follows:
Uncaught Error: Syntax error, unrecognized expression: input:checkbox[name=food[]]
at Function.se.error (jquery-3.5.1.slim.min.js:2)
at se.tokenize (jquery-3.5.1.slim.min.js:2)
at se.select (jquery-3.5.1.slim.min.js:2)
at Function.se [as find] (jquery-3.5.1.slim.min.js:2)
at E.fn.init.find (jquery-3.5.1.slim.min.js:2)
at new E.fn.init (jquery-3.5.1.slim.min.js:2)
at E (jquery-3.5.1.slim.min.js:2)
at HTMLInputElement. (quiz.js:43)
at HTMLInputElement.dispatch (jquery-3.5.1.slim.min.js:2)
at HTMLInputElement.v.handle (jquery-3.5.1.slim.min.js:2)
se.error @ jquery-3.5.1.slim.min.js:2
se.tokenize @ jquery-3.5.1.slim.min.js:2
se.select @ jquery-3.5.1.slim.min.js:2
se @ jquery-3.5.1.slim.min.js:2
find @ jquery-3.5.1.slim.min.js:2
E.fn.init @ jquery-3.5.1.slim.min.js:2
E @ jquery-3.5.1.slim.min.js:2
(anonymous) @ quiz.js:43
dispatch @ jquery-3.5.1.slim.min.js:2
v.handle @ jquery-3.5.1.slim.min.js:2