Pass the result of a variable to quotes

0

I need to pass the result of a variable and that it is enclosed in quotes.

document.getElementById("plano").addEventListener("keyup", blueprintControl);

function blueprintControl(event){

    var id = event.srcElement.getAttribute("id");
    var valor = $('#'+id).val();
    doSearch($(id);
    $(id).html( "<label>"+id+valor+ "</label>" );

}

result of variable id = identificador

I need now to pass the id value so that it is within ""

id_convert = "identificador"

I do not know if this is possible with jQuery or JavaScript. I have tried everything and I have not been able to achieve it.

    
asked by Darwin Gomez 24.11.2017 в 05:03
source

1 answer

1

I think you better mean to introduce a variable to concatenate it, if so, I'll leave you 2 ways, otherwise I'll leave you another one to add quotes.

var color = "red", p = document.getElementsByTagName("p")[0],
    color2 = "blue", b = document.getElementsByTagName("b")[0];


/* 1º Introducir variable con "+nombrevariable+" , 
   nota: las comillas deben ser igual que las que usas, osea:
   si usaste "a" para meter otra variable usas las mismas comillas,  
   en este caso comillas dobles " a "+var+" ";
     
*/
b.setAttribute("style","background-color: "+color+";"); 

/* 2º Strings literales [ Nuevas con ES6 ] 
Funcionan con: '' y para introducirle variables usas:

  ${var} o además puedes hacer operaciones como ${4+3} o hasta    ejecutar una función !
*/

p.setAttribute("style",'color: ${color2}');
<b>holaaa</b>
<p>Hola</p>

Here is the method to add single or double quotes:

function add(v,w){
 return w === "d" ? '"${v}"' : ''${v}'';
}

var a = "identificador";
console.log(add(a));
 // Si el segundo parámetro es "d" , serán comillas dobles, de lo contrario no envies el segundo parámetro y seran simples.
    
answered by 24.11.2017 в 11:40