-1

Here is my code:

var iconArray = ["icon01", "icon02"];
var iconWidth = $("'." + iconArray[0] + "'").css('width');

When I try this code, I get the error message:

Error: Syntax error, unrecognized expression: '.icon01' @ jquery-v1-10-0.js:1916

This is evidently a sizzle.error in the jQuery code.

I am wanting to extract the width from the css of a large array of icons to use in a function. The code is successfully converting the text in the () to '.icon01', but it is generating the error mentioned above.

I am relatively new to coding, so please forgive me if my formatting style is not up to par, but please suggest any needed style changes. Thanks in advance.

Jason
  • 11
  • 5

2 Answers2

4

This is the way to use variable string in selector:

var iconWidth = $('.' + iconArray[0]).css('width');
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
2

You just need this -

$('.'+iconArray[0]).css('width');
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111