Hello, good morning, I have a problem when I want to pass my variable to the function as a parameter. I attach my code.
Code
var fruta = "hola";
var html = '';
html += "<div class='card direct-chat direct-chat-primary'>"+
"<div class='card-header'>"+
"<h3 class='card-title'>Comentarios</h3>"+
"</div>"+
"<div class='card-body'>"+
"<div class='direct-chat-messages'>"+
comentarios+
"</div>"+
"</div>"+
"<div class='card-footer'>"+
"<form>"+
"<div class='input-group'>"+
"<input type='text' id='form-input-comentario' name='message' placeholder='Comentario ...' class='form-control'>"+
"<span class='input-group-append'>"+
"<button type='button' class='btn btn-primary' onclick='set_comentario_ajax("+frutas+");'>Enviar</button>"+
"</span>"+
"</div>"+
"</form>"+
"</div>"+
"</div>"+
"<div class='custom-file'>"+
"<input type='file' class='custom-file-input' id='customFile'>"+
"<label class='custom-file-label' for='customFile'>Seleccione</label>"+
"</div>";
$('#form-ticket-comentarios').html(html);
Function I want to call
function set_comentario_ajax(data){
var porId = document.getElementById("form-input-comentario").value;
alert(porId);
console.log(data);
}
Mistake
ReferenceError: hello is not defined Tickets:1:1
I guess I'm wrongly concatenating my variable "fruits" as a parameter to the function
Avoid concatenating content, it is always the source of errors like the one you have now, instead, use text templates , enclosing the content between backticks and
`
, each time you need to include a variable, you only need${nombre_de_variable}
The problem is that you are sending a variable that does not exist in the html, so you should add some quotes,
To be specified from JavaScript as a string
I also recommend you use ${} inside where you want to insert a variable, it must go inside these quotes (``)
It is not necessary to use ";" on click
Try adding the parameter with single quotes: