Seppia Anthony Seppia Anthony - 2 months ago 12
jQuery Question

jQuery, pass a variable to append()

This one works, actually:

$('.open-submenu > a').append("<span class='open-submenu-sym'>+</span>");

I would like to change the former to:

var open-string = "<span class='open-submenu-sym'>+</span>";
$('.open-submenu > a').append(open-string);

but it seems not working...I tried also
$('.open-submenu > a').append('' + open-string);
$('.open-submenu > a').append("" + open-string);
but still doesn't it possible to accomplish that?


The variable name is invalid since it includes - in your variable name. To make it valid replace the - character from the variable name.

var open_string = "<span class='open-submenu-sym'>+</span>";
$('.open-submenu > a').append(open_string);

For more info : What characters are valid for JavaScript variable names?

MDN Docs : Variables