JAVASCRIPT concatenate

Asked

Viewed 270 times

-1

You’re making a mistake on this line, how do I concatenate it? The reason I’m doing this is that I want the user to click invoke the function of deleting the cd and pass the ID!

 html += '<a onclick=excluirCD('+rows[i].id_cd+')>'excluir'</a>';
  • 1

    That one rows[i].id_cd is a number or string? and excluir is text or a variable?

  • I apologize @A Programmer

  • It is a number @Sergio, I managed to solve this way:

  • I mean, I could have solved it that way too? html += '<a onclick=excluirCD('+rows[i].id_cd+')>excluir</a>'; I’m thinking this is a typo, you had quotes where I shouldn’t.

  • I managed to solve, how do I take my function to delete this ID being passed by calling the function onclick=excluirCD

2 answers

1

If deleting is text, then you don’t need quotes and you can do so:

html += '<a onclick=excluirCD(' + rows[i].id_cd + ')>excluir</a>';
  • Thanks @Sergio, how do I take my function to delete this ID being passed by calling the onclick=excluirCD function?

  • @Marcosricardo this is an HTML table only with data from rows right? and that ID is only right?

0

//Função que realiza select no banco para mostrar todos os cd´s registrados
//consegui resolver desta forma
function selecionarDadosDoBanco() {       
    conexaoDB();        

    $query = 'select * from tbl_cd';
    conexao.query($query, function(err, rows, fields) {
        if(err){
            console.log("erro ao selecionar dados do banco para amostragem");
            console.log(err);
            return;
        }    

        var html = '';
        var id = '';
        for (var i = 0; i < rows.length; i++) {
            html += '<tr>';
            html += '<td>';
            html += rows[i].nomecd;
            html += '</td>';
            html += '<td>';
            html += rows[i].preco;
            html += '</td>';  
            html += '<td>';
            html += rows[i].data;
            html += '</td>'; 
            html += '<td>';
            html += '<a href="onclick=excluirCD('+rows[i].id_cd+')">';
            html += 'excluir';
            html += '</a>'; 
            html += '</td>'; 
            html += '</tr>';                
        }

        $("#tblCd tr").after(html);
    });
    conexao.end(function(){});
}
selecionarDadosDoBanco();

//como faço para pegar este id na outra funcção agora???
function excluirCD() {      
    var id = ?

    //Verifica se um cd foi acionado
    if(id == null)
    {
        alert('erro ao tentar excluir!');
    }
    else
    {
        conexaoDB();

        con.query('DELETE FROM tbl_contatos WHERE ?', [{"id_cd" : id}],  function(err, rows, fields) {
            if(err){
                console.log("erro DELETE.");
                console.log(err);
                return;
            }
        });

        connection.end(function(){});
    }
}

Browser other questions tagged

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