Is this the Correct Way to Access Array Elements in jQuery

0 votes

I have two arrays. One is for a navigation, the other is for panels on the page. The arrays are both the same size. One nav button for one panel. This code works, but I'm sure there must be a better way to do this without setting up the temporary variables.

$('.footer-nav li').click(function()
{
  var temp = $('.footer-nav li').index(this);
  var tArray = $('.about-bgs li');
  $('.about-bgs li').fadeOut();
  $(tArray[temp]).fadeIn();  //This is the line in question!
});

Any takers?

Jun 13 in JQuery by gaurav
• 12,420 points
4 views

1 answer to this question.

0 votes

Array elements are accessed by using an integer index. Array index starts with 0 and goes till size of array minus 1.

We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.

Advantages of an Array in C/C++: 

  1. Random access of elements using array index.
  2. Use of fewer line of code as it creates a single array of multiple elements.
  3. Easy access to all the elements.
  4. Traversal through the array becomes easy using a single loop.
  5. Sorting becomes easy as it can be accomplished by writing fewer line of code.
answered Jun 13 by rajatha
• 3,540 points

Related Questions In JQuery

0 votes
1 answer

What is the official correct way to declare jQuery variables?

Declare a variable using the var keyword. Initialize ...READ MORE

answered Jun 2 in JQuery by Edureka
• 13,600 points
18 views
0 votes
1 answer

What is the equivalent of jQuery .hide() to set visibility: hidden

Hello Kartik, There isn't one built in but ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,720 points
5,385 views
0 votes
1 answer

How to check if button is disabled in jQuery Mobile?

Hello, you can try $("#deliveryNext").is(":disabled") The following code works for ...READ MORE

answered May 29, 2020 in JQuery by Niroj
• 82,720 points
2,062 views
0 votes
1 answer

How do I check if the mouse is over an element in jQuery?

Hello @kartik, Use this code: $("someelement").mouseenter(function(){ ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,720 points
891 views
0 votes
0 answers

how do I create an array in jquery?

$(document).ready(function() { $("a").click(function() { ...READ MORE

Jun 6 in JQuery by Edureka
• 13,600 points
14 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,900 points
2,816 views
0 votes
2 answers

How does random shuffling of an array

Here is a simple way using an ArrayList: List<Integer> ...READ MORE

answered Nov 2, 2018 in Java by Sushmita
• 6,900 points
238 views
0 votes
1 answer

Numpy: Multiplying large arrays with dtype=int8 is SLOW

Unfortunately, as correctly underlined in the comments, the ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 58,080 points
1,468 views
0 votes
1 answer

Best way to unselect a <select> in jQuery?

The correct approaches are either . val([]) or . prop("selected", ...READ MORE

answered Jun 13 in JQuery by rajatha
• 3,540 points
7 views
0 votes
1 answer

jquery function val() is not equivalent to "$(this).value="?

val() method is primarily used to get the ...READ MORE

answered Jun 14 in JQuery by rajatha
• 3,540 points
5 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP