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, 2022 in JQuery by gaurav
• 23,260 points
1,119 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, 2022 by rajatha
• 7,640 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, 2022 in JQuery by Edureka
• 13,670 points
5,088 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,880 points
13,020 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,880 points
4,081 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,880 points
2,028 views
0 votes
0 answers

how do I create an array in jquery?

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

Jun 6, 2022 in JQuery by Edureka
• 13,670 points
358 views
0 votes
1 answer

How to loop through array in jQuery?

Generic loop: var i; for (i = 0; i ...READ MORE

answered Jun 28, 2022 in Web Development by rajatha
• 7,640 points
1,851 views
0 votes
0 answers

Jquery array.push() not working

I have been trying to add variables ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
1,320 views
0 votes
0 answers

jQuery JSON Decode ( PHP to Javascript)

I'm trying to make an autocomplete script. ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
405 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, 2022 in JQuery by rajatha
• 7,640 points
2,899 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, 2022 in JQuery by rajatha
• 7,640 points
2,118 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP