1

I want the select name array keys have a quotations inside. But this one doesn't generate quotations inside the brackets. How can I achieve it?

var id = "test";
var element = "<select name = 'unit_id["+id+"]' class = 'form-control'></select>";

2 Answers 2

1

You can use Template literals which is more cleaner. This does not require string concatenation and character escaping:

var id = "test";
var element = `<select name = 'unit_id["${id}"]' class = 'form-control'></select>`;
console.log(element)

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

4 Comments

*cleaner. It's recommended to point out ES6+ incompatibilities.
@JuanElfers, I will request you update your answer mentioning such incompatibilities........and encouraging OP not to use this wonderful feature....
Mamun, I don't need to update my answer because it doesn't use template literals. It's as simple as that. We can talk about Babel if you want, but I think that is outside the scope of this topic. Have a good day.
@JuanElfers, still you can suggest the reader of your answer not use template literals...and sorry there is no babel tag in the question....
0

Like this:

var id = "test";
var elemnt = "<select name=\"unit_id['" + id + "']\" class=\"form-control\"></select>";

2 Comments

This works too, but cant accept 2 answers. But thanks for time and effort
I hope your code doesn't go for a production website, because template literals have limited support amongst browsers: caniuse.com/#feat=template-literals

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.