3

I have a piece of code which looks like the following,

$(document).ready(function(){
  var agr1='#19865 - testArg';
  var arg2=1856
  $('#dv_Test').append('<button onclick="testFunction('+ agr1 +','+ arg2 +')">Test</button>')
});
function testFunction(arg1, arg2) { 
  alert(arg1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dv_Test"></div>

here I am getting error like the following

testFunction(#19865 - testArg,1856)  

Uncaught SyntaxError: Invalid or unexpected token

and my string agr1 doesn't have "" with itself

how can I solve this issue?

4
  • But why are you using inline handlers? $('<button />').click(function() { // access scope variables normally}) Commented Jan 2, 2019 at 12:02
  • this is just an example I am getting data from database and binding this to table Commented Jan 2, 2019 at 12:03
  • your inline arguments should be strings if they are meant to be so. In a nutshell, you need to escape them, hence the "invalid or unexpected token" exception. Commented Jan 2, 2019 at 12:03
  • 2
    @MumbaiWadala " I am getting data from database and binding this to table" it doesn't really matter if you have access to the data (and I assume you do :)) You don't need inline handlers in year 2019. Commented Jan 2, 2019 at 12:05

2 Answers 2

5

The issue is because the arg1 value is a string and needs to have quotes around it:

$(document).ready(function() {
  var agr1 = '#19865 - testArg';
  var arg2 = 1856
  $('#dv_Test').append('<button onclick="testFunction(\'' + agr1 + '\',' + arg2 + ')">Test</button>')
});

function testFunction(arg1, arg2) {
  alert(arg1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dv_Test"></div>

However a much better way to do this would be to put those values in data attributes and add a single delegated event handler to handle all dynamically created buttons. Try this:

$(document).ready(function() {
  var agr1 = '#19865 - testArg';
  var arg2 = 1856
  var $div = $('#dv_Test').append('<button data-arg1="' + agr1 + '" data-arg2="' + arg2 + '">Test</button>');
  
  $div.on('click', 'button', function() {
    var $btn = $(this); 
    console.log($btn.data('arg1'), $btn.data('arg2'));
  }); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dv_Test"></div>

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

2 Comments

thanks the second solution is really good and I will implement that.
No problem, glad to help
1

You need to have quotes around the argument.Alternatively you can try template literals

$(document).ready(function() {
  var agr1 = '#19865 - testArg';
  var arg2 = 1856
  $('#dv_Test').append(`<button onclick="testFunction('${agr1}','${arg2}' )">Test</button>`)
});

function testFunction(arg1, arg2) {
  alert(arg1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dv_Test"></div>

Comments

Your Answer

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