Event "onclick" within variable and Uncaught SyntaxError: Unexpected token}

0

My idea is to add rows to a table with an "onclick" event in them. As you can see, in "var table" I create the "tr, td" and I pass the styles. So far, no problem, but when I launch it gives me an error of the type "Uncaught SyntaxError: Unexpected token}"

It seems that everything is a matter of the way I add the "onclick" to "table", because passing values by hand to insertPrefix (prefixFinal) works and "pickPrefixes" paints all the rows of the table well; it only fails when I click on any of the rows giving me the error mentioned above. The values "revision, revision2 and revision3" are strings, so I do not think that is the problem.

HTML:

 <div class="modal-body">
                    <div class="container">
                        <h2>Filtro de Prefijos</h2>
                        <p>Escriba en el campo "Buscar" para realizar un filtrado de opciones</p>
                        <input class="form-control" id="myInputPrefix" type="text" placeholder="Buscar..">
                        <br>
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>Resultados</th>
                                </tr>
                            </thead>
                            <tbody id="myTablePrefix">

                            </tbody>
                        </table>
                    </div>
                </div>

I have the following functions:

  function recogerPrefijos(revision, revision2, revision3) {
        var prefijoFinal = "PREFIX" + revision + ":" + revision2;
        prefijoFinal = prefijoFinal.toString();
        var tabla = "<tr><td style=\"max-width:50px; padding:10px;\"><a class=\"list-group-item list-group-item-action primary-color\" id=\"list-home-list\" data-toggle=\"list\" href=\"#list-home\" role=\"tab\" aria-controls=\"home\" title=\"Select\" data-dismiss=\"modal\" href=\"#\" onclick=\"insertarLinkPrefijo(\" + prefijoFinal + \")\">" + revision + ": " + revision2 + "</a></td></tr>";
        var table = document.getElementById("myTablePrefix");
        var row = table.insertRow(contadorRecogerPrefijos);
        row.innerHTML = tabla;
        contadorRecogerPrefijos++;

        return prefijoFinal;
    }


function insertarLinkPrefijo(prefijoFinal) {
        var sentencia = prefijoFinal;
        var editor = document.getElementsByClassName("CodeMirror-line");
        editor[0].textContent = sentencia;
    }

Thank you very much

    
asked by Jonan87 30.01.2018 в 16:06
source

1 answer

0

I would lack data to answer you with total certainty, but perhaps it is that you have badly codified all the string that goes in the table with the onclick. Here I leave the correction.

var tabla = "<tr><td style=\"max-width:50px; padding:10px;\"><a class=\"list-group-item list-group-item-action primary-color\" id=\"list-home-list\" data-toggle=\"list\" href=\"#list-home\" role=\"tab\" aria-controls=\"home\" title=\"Select\" data-dismiss=\"modal\" href=\"#\" onclick=\"insertarLinkPrefijo('" + prefijoFinal + "')\">" + revision + ": " + revision2 + "</a></td></tr>";
    
answered by 30.01.2018 / 16:30
source