Hi, I'm trying to create some buttons that make show
and hide
according to the category but I have some error and it does not work.
This is the for
that mounts the buttons:
for (var i = 0; i < data.length; i++) {
var categoria = data[i].NombreCategoria;
if (categoria != null) {
categoria = categoria.split(" ").join("");
}
else {
categoria = "null";
data[i].NombreCategoria = "Sin categoría";
}
$("#ContenedorConvenios").append(""
+ "<button class='btn btn-eco' onclick='prueba(" + categoria.toString() + ");' id='" + categoria + "'>" + data[i].NombreCategoria + "</button>"
+ "");
console.log("Hola" +categoria);
}
This is the code for the toggle:
function prueba(cat) {
console.log("adios" + cat);
$('#'+cat).on('click', function () {
$('.' +cat).toggle()
});
}
and this is what the console log returns to me:
adios[object HTMLButtonElement] jquery-1.11.1.min.js:2 Uncaught Error:
Syntax error, unrecognized expression: #[object HTMLButtonElement]
at Function.fb.error (jquery-1.11.1.min.js:2)
at fb.tokenize (jquery-1.11.1.min.js:2)
at fb.select (jquery-1.11.1.min.js:2)
at Function.fb [as find] (jquery-1.11.1.min.js:2)
at m.fn.init.find (jquery-1.11.1.min.js:2)
at m.fn.init (jquery-1.11.1.min.js:2)
at m (jquery-1.11.1.min.js:2)
at prueba (custom.js:195)
at HTMLButtonElement.onclick (acuerdos-comerciales.html:1)
adios[object HTMLButtonElement]
means that it picks up the whole button.
How do I just collect the category?
thanks!
Solved many thanks! Now I have the problem that I want to do toggle but with a single click and I do not know how to do it, can someone guide me?