Larson B Larson B - 5 months ago 23
Javascript Question

Jquery attribute selector using string concatenation in version 1.12.1

I want to use a variable as a selector attribute.

This is how I used to do this.

var variable = 'string';
$('[href=#'+variable+']'); => $('href=#string')


But in jquery version 1.12.1, this is no longer supported.
https://github.com/jquery/jquery/issues/2824

So how would I refactor this without making it look too ugly?

Answer

Just add quotes around the internal link and it should work.

$('[href="#'+variable+'"]'); => $('href="#string"')
Comments