Search code examples
jquerycssarrayssizzle

How do I insert array values into a jQuery statement: $(arrayValue).css('width');


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.


Solution

  • This is the way to use variable string in selector:

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