0

function show(array){
  console.log(array);
}

function main() {
  var send = ["HOLA", "ADIOS"];
  $('#table').append('<tr>'+
                    '<td style="width: 35%">Oscar</td>'+
                    '<td style="width: 6%">'+
                        '<button type="button" class="btn btn-success" data-dismiss="modal" onclick=show('+send+')>+</button>'+
                    '</td>'+
                '</tr>'
   );
}
main();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table" class="table table-bordered">
	<thead>
		<tr>
		  <th style="width: 35%" id="wallet-modal-table-name-header"></th>
			<th style="width: 6%"  id="wallet-modal-table-options-header"></th>
		</tr>
	</thead>
</table>

I have this code and I want to send the array "send" but when I send it as a parameter in the functions it shows me an error. What could I do?

2
  • 1
    Embrace the function addEventListener(event, handler) your approach is concatenating the object/array rather than actually passing that array as param. Commented May 15, 2018 at 18:27
  • Could you upload a snippet please Commented May 15, 2018 at 18:28

2 Answers 2

1

For dynamically created elements, use the Event delegation approach:

$(selector4parent).on(event, selector4children, handler);

function show(array) {
  console.log(array);
}

function main() {
  $('#table').on('click', '[type="button"]', function(e) {
    var send = ["HOLA", "ADIOS"];
    show(send);
  });


  $('#table').append('<tr>' +
    '<td style="width: 35%">Oscar</td>' +
    '<td style="width: 6%">' +
    '<button type="button" class="btn btn-success" data-dismiss="modal">+</button>' +
    '</td>' +
    '</tr>'
  );
}
main();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table" class="table table-bordered">
  <thead>
    <tr>
      <th style="width: 35%" id="wallet-modal-table-name-header"></th>
      <th style="width: 6%" id="wallet-modal-table-options-header"></th>
    </tr>
  </thead>
</table>

Sign up to request clarification or add additional context in comments.

2 Comments

I try your code but I'm having problems because I have a for and inside the for I'm calling the function send. send(x[i])
@OscarOrdoñez can you post that logic?
0

You can create your button outside of the string then append it to the TD you would like it in.

var $btn = $(document.createElement("BUTTON"));
$btn.attr({ 'type': 'button', 'data-dismiss': 'modal'}).addClass('btn btn-success');

$btn.click(function()
{
    show(send);
});

$('td-selector').append($btn);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.