Concatenate strings in the parameters of a function?

Asked

Viewed 48 times

0

I need to inject the v as simple quotation marks 'v' in the concatenation of código

onClick="validaContato(1,'v') // resultado desejado no segundo parametro

code

 var botao =  '<div class="dropdown">'
     botao +='<a class="dropdown-item  " href="#" onClick="validaContato('+response.id+',"v");"><i class="la la-bell "></i> Valido</a>'
     botao +='<a class="dropdown-item " href="#" onClick="validaContato('+response.id+',"i");"><i class="la la-cloud-upload"></i>Inválido</a>'
     botao += '</div> </div>'
  • You can show the function validaContato?

1 answer

3


Why not use literals template ?

This simplifies it a lot, because not only is it multiline, but it doesn’t have to escape " or '. In addition you use interpolation with ${} rather than concatenate with +.

Now look:

var botao = 
    `<div class="dropdown">
        <a class="dropdown-item" href="#" onClick="validaContato(${response.id},'${v}');">
            <i class="la la-bell"></i>Valido
        </a>
        <a class="dropdown-item" href="#" onClick="validaContato(${response.id},'${i}');">
            <i class="la la-cloud-upload"></i>Inválido
        </a>
     </div> 
</div>`;

Note that I kept the structure, but you have a clasp </div> the most.

Now if you want to keep the style you have just do so:

onClick="validaContato('+response.id+',"' + v + '")
  • Thank you very much Isac, sanou my problem and I will use the crases, much more quiet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.