Quotation marks within quotation marks and again within other quotation marks?

1

I have a string variable called [Content] and I want to assign it a button tag that calls a javascript. I'm trying like this:

        Conteudo = "<input id='btnOculta_" + lin + "' type='button' value='novo' class='btn btn-default' onclick='Ocultar(this, 'OcultaNovoMedicamento_1')' style='float:left' />"

But on the onclick = 'Hide (this,' occultNewMedication_1 ')' snippet it is the second 'as a closure of the first and not as a second opening. And it returns me a wrong code.

Expected:

<input id="btnOculta_1" type="button" value="novo" class="btn btn-default" onclick="Ocultar(this, 'OcultaNovoMedicamento_1')" style="float:left" />

Got (Wrong):

<input class="btn btn-default" id="btnOculta_1" style="float:left"         onclick="Ocultar(this, " type="button" value="novo" ocultanovomedicamento_1')'="">

How do I solve this cucumber?

    
asked by anonymous 31.08.2017 / 16:04

2 answers

4

You can use \ to escape these quotes and do so:

onclick='Ocultar(this, \"OcultaNovoMedicamento_1\")'

Example:

var lin = 2;
var Conteudo = "<input id='btnOculta_" + lin + "' type='button' value='novo' class='btn btn-default' onclick='Ocultar(this, \"OcultaNovoMedicamento_1\")' style='float:left' />";

document.body.innerHTML += Conteudo;

function Ocultar(el, txt) {
  console.log(el.tagName, txt);
}

In modern browsers, or if you're transpiling, you can use template strings like this:

Conteudo = '<input id='btnOculta_${lin}' type='button' value='novo' class='btn btn-default' onclick='Ocultar(this, "OcultaNovoMedicamento_1")' style='float:left' />'
    
31.08.2017 / 16:06
1

Attempts to use escaped double quotation marks in OcultaNewMedication_1:

Conteudo = "<input id='btnOculta_'  type='button' value='novo' class='btn btn-default' onclick='Ocultar(this, \"OcultaNovoMedicamento_1\")' style='float:left' />"
    
31.08.2017 / 16:11