Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Access elements in array

Tags:

arrays

jquery

I need to be able to get the width of elements from an array

HTML

<div id="container">
  <ul>
     <li id="one">--------</li><br />
     <li id="two">----------------</li><br />
     <li id="three">-------</li><br />
  </ul>
</div>

JS

I know i can access the individual width's like this

$('#one').width();

But in an array

var $array = $("#container li");

How do i access a specific width of the element by its index

e.g

$array[2].width(); //which causes error

Example http://jsfiddle.net/8zvkn/

like image 905
eleven11 Avatar asked Jan 18 '13 21:01

eleven11


2 Answers

Use eq :

$('#container li').eq(i)
like image 106
Denys Séguret Avatar answered Nov 11 '22 11:11

Denys Séguret


You can use .eq function like below,

$array.eq(2).width()

DEMO: http://jsfiddle.net/8zvkn/2/

$array[2] - returns DOM element but what you need is the jQuery object which has the .width function.

like image 38
Selvakumar Arumugam Avatar answered Nov 11 '22 10:11

Selvakumar Arumugam