How to call a js function in html

Asked

Viewed 101 times

0

Good I’m not sure but I think the error is in the form in which I am calling the js function. Follow the code:

createdCell: function(td, cellData, rowData, row, col){
                    //td:       a coluna em questão
                    //cellData: dados da linha inteira
                    //rowData:  dados da linha inteira
                    //row:      index da linha
                    //col:      index da coluna


                    $(td).html(`<a class="btn btn-primary" onclick='${editar(cellData,row)}'>Editar</a>`); 
                }

This way when I open the page it already opens the change modal that should fire only when the button is clicked. Code of the function I’m calling:

var editar = function( dados2, linha2 ) {
var dados = dt.data();
console.log(dados2);
console.log(linha2);
$( '#alterarModal' ).modal();
for ( var i in dados ) {

    if ( i == linha2) {
        $( '#alt_nome' ).val( dados2.nome );
        $( '#alt_siape' ).val( dados2.siape );
        $( '#alt_cpf' ).val( dados2.cpf );
        $( '#alt_funcao' ).val( dados2.funcao );
        $( '#alt_codigo' ).val( dados2.codigo );
        $( '#alt_nivel' ).val( dados2.nivel );
        $( '#alt_unidade' ).val( dados2.unidade );
        $( '#alt_portaria' ).val( dados2.portaria );
        $( '#alt_publicacao' ).val( dados2.publicacao );
        $( '#alt_ingresso' ).val( dados2.ingresso );
        $( '#alt_exoneracao' ).val( dados2.exoneracao );
        $( '#alt_sub_nome' ).val( dados2.subnome );
        $( '#alt_sub_siape' ).val( dados2.subsiape );
        $( '#alt_sub_cpf' ).val( dados2.subcpf );
        $( '#alt_sub_ingresso' ).val( dados2.subingresso );
        $( '#alt_sub_exoneracao' ).val( dados2.subexoneracao );
        $( '#alt_sub_publicacao' ).val( dados2.subpublicacao );
        break;
    }
}
  • I don’t understand what you want to do with that code... you can explain what you want to do?

1 answer

2

You are mounting HTML inside a string template, so whatever you have of JS in the middle, enter ${}, will be executed. You must change:

${editar(cellData,row)}

for

editar(${cellData},${row}).

And depending on the types of cellData and row maybe you need simple quotes around them, in the final output.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.