Jquery syntax error [closed]

-1

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?

    
asked by kaiCo 30.08.2017 в 12:02
source

2 answers

-1

It is possible that it is by the quotes of "goodbye", test with single quotes:

function prueba(cat) {
    console.log('adios' + cat);
    $('#'+cat).on('click', function () {
        $('.' +cat).toggle()
    });
}

Does the category you pick have a quote and is the syntax failing?

    
answered by 30.08.2017 / 12:07
source
0

Actually the error is because you are missing the quotes in the test argument:

var data = [{NombreCategoria: 'categoria1'}, {NombreCategoria: 'categoria2'}, {NombreCategoria: 'categoria3'}];

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);
}

function prueba(cat) {
    console.log("adios" + cat);
    $('#'+cat).on('click', function () {
        $('.' +cat).toggle()
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="ContenedorConvenios"></div>
    
answered by 30.08.2017 в 12:15