How do I get an onclick inside append ()?

1

I try to make an action onclick within a append . Here is my example:

$('#tr').append("<td><a onclick='nombre(a,b)'></a></td>")

by clicking on the link I want to pass some parameters. I already tried like this. I did my job like this:

function nombre(a,b){}

but I can not get it to work. Maybe it's not the right way. I need help! If someone can explain more in detail how to solve this problem.

    
asked by Cristian Hernandez 14.06.2017 в 18:40
source

6 answers

1

an example using Jquery you can capture all the clicks of the buttons whose class is buttons

$('#tabla').append('<tr><td><button class="botones">Nombre del boton</button></td></tr>');
$('#tabla').append('<tr><td><button class="botones">Super Boton</button></td></tr>');
$('#tabla').append('<tr><td><a class="botones">href 1</a></td></tr>');
$('#tabla').append('<tr><td><a class="botones">href 2</a></td></tr>');
$(".botones").click(function() {
  console.log($(this).text());
//tu funcion con los parametros
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tabla">
</table>
    
answered by 14.06.2017 / 20:09
source
2
$(document).ready(function(){
  function nombre(a,b)
  {
    ...
  }
  $('#tr').append("<td><a class='btn'></a></td>")
  $(document).on('click','.btn',function(){
    nombre(a,b);
  });
});

You do not need to change your tag to a button.

Greetings.

    
answered by 14.06.2017 в 18:54
1

Not the best way to do it because of the following:

  • The append has to parse all the HTML that you pass as a string and the larger the string the more you will stick to the performance.
  • The function that you are passing to the onclick needs the variables to and b as parameters, however when creating the event the values of those variables will be lost, unless they are global variables.
  • Maintenance is going to be tedious

Take into account that by not putting an anchor text (the link) there will not be an element to click on

You can create each element separately and that way you can manipulate them as best suits you

function agregaLink(){
  var a = "Hola";   //a puede ser local
  var b = "Mundo";  //b puede ser local
  var td;
  var link;

  //Creamos el anchor con el texto 'click aqui' y el evento click que invoca a nombre(a,b)
  link = $("<a>").html("click aqui").click(function(){ nombre(a,b) });
  //Creamos el td y le agregamos el anchor
  td = $("<td>").append(link);
  //Al elemento con id='tr' le agregamos el td
  $('#tr').append(td);
}

function nombre(a,b){
   console.log(a+" "+b);
}

$(document).ready(function(){ agregaLink(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr id="tr"> </tr>
</table>
    
answered by 16.06.2017 в 23:42
0

And if you change this:

$('#tr').append("<td><a onclick='nombre(a,b)'></a></td>")

Because of this. If you are going to have several append, use a class "td-link" instead of an id:

$('#tr').append("<td><a id='enlace-td' data-param0="dato1" data-param1="dato2"></a></td>")
$("#enlace-td").click(nombre);

For the function name:

function nombre(ev) {
    // currentTarget apunta al propio elemento al que enlazaste el evento.
    var param0 = $(ev.currentTarget).attr("data-param0"),
    param1 = $(ev.currentTarget).attr("data-param1");
    console.log(param0);
    console.log(param1);
}
    
answered by 14.06.2017 в 18:53
0

Try a button tag:

$('#tabla').append('<tr><td><button onclick="nombre()">Nombre del boton</button></td></tr>');

#tabla = id de la tabla

Try creating a function and sending hard data onclick="name (1)"

function nombre(valor){
    alert(valor);
}

With this you check that your onclick is working

    
answered by 14.06.2017 в 18:52
0

Your code has no errors but you may be confusing some things.

  • I see that you use a ID selector whose name is the same as your TAG , since you do not show the HTML tag this tends to confuse.
  • You use the variables a and b but I do not see where you define them.
  • Otherwise your code is correct, I leave a functional example for you to compare with yours where I put the points: IMPORTANT !! so you can see where the error is.

    Example:

    // IMPORTANTE!! Definir las variables a y b
    var a,b;
    
    $('tr').append("<td><a onclick='nombre(a,b)'>Link</a></td>");
    
    function nombre(a,b){
     console.log("hola"); // IMPORTANTE!! acción a realizar en la función
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
     <tr id="tr"><!-- IMPORTANTE!! definir el id="tr" -->
     </tr>
    </table>
        
    answered by 14.06.2017 в 21:30