+ 10

[SOLVED] Class Selector with jQuery

I have a number of elements with the class name "progressor". I have in the JavaScript a global variable that I want to store the elements of this class name like so: let bars = $(".progressor"); Later on I try to access this array and change the width property of the respective element, but it says that the jQuery width function is not a function at all. Like so: bars[barNum].width("75%"); Error: Uncaught TypeError: bars[barNum].width is not a function Using bars[barNum].css("width", "75%"); doesn't work either. Same error? Am I missing a simple jQuery thing here? Edit: just to note, using document.getElementsByClassName("progressor") works by using: bars[barNum].style.width = "75%";

26th Jun 2018, 4:03 AM
Zeke Williams
Zeke Williams - avatar
6 Réponses
+ 9
For accessing specific elements within an array of class elements in JQuery, it seems that you would need to wrap the identifier within $() for it to work. $(bars[barNum]).css("width","75%"); //Seemed to run This is a stackoverflow question related to this that may also help you out - https://stackoverflow.com/questions/10479879/how-to-return-array-of-jquery-object-with-selector
26th Jun 2018, 4:41 AM
Faisal
Faisal - avatar
+ 8
You can use jQuery's .eq() method to get the element with a certain index.
26th Jun 2018, 4:48 AM
Calviղ
Calviղ - avatar
+ 6
Thank you Faisal ! I looked on stack overflow using a similar search, but came up with other answers.
26th Jun 2018, 5:31 AM
Zeke Williams
Zeke Williams - avatar
+ 5
Zeke Williams You're most welcome. 😏
26th Jun 2018, 6:00 AM
Calviղ
Calviղ - avatar
0
For accessing specific elements within an array of class elements in JQuery, it seems that you would need to wrap the identifier within $() for it to work. $(bars[barNum]).css("width","75%"); //Seemed to run This is a stackoverflow question related to this that may also help you out - https://stackoverflow.com/questions/10479879/how-to-return-array-of-jquery-object-with-selector x2
27th Jun 2018, 3:44 AM
{{ оᴉяᴀϻоя }}
{{ оᴉяᴀϻоя }} - avatar
14th Jul 2018, 9:37 PM
Karina Bowman
Karina Bowman - avatar