K
K
kodenji2018-12-21 15:49:39
JavaScript
kodenji, 2018-12-21 15:49:39

How to concatenate strings if the string contains a single quote?

vue.js has a template

id = "'" + obj[i].id + "'"
symbol = "'" + obj[i].symbol + "'"
name = '"' + obj[i].name + '"'

In name there is such a string: BlaBla's bar - there is a single quote, I put the string in double quotes, the code is above
template += '<tr>'
template += '<td><a href="#" @click.prevent="addRow(' + id + ',' + symbol + ',' + name + ')">Add to Row</a></td>'
template += '</tr>'

The template does not compile I tried a bunch of options, googled, but no more strength)
BlaBla's bar")">Add to Row

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2018-12-21
@kodenji

var kav = "'"
params = [obj[i].id, obj[i].symbol, obj[i].name].map(
    v => kav + ('' + v).replace(kav, "\\'") + kav
)

template += '<td><a href="#" @click.prevent="addRow(' 
    + params + ')">Add to Row</a></td>'

A
Anton Anton, 2018-12-21
@Fragster

https://learn.javascript.ru/string
If you are too lazy to set up the environment to use single-file components , switch to X-Templates
In general, judging by how the template text is formed, you somehow did not understand why vue is needed at all.

N
nvdfxx, 2018-12-21
@nvdfxx

read about template strings, interesting information, they say

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question