When pressing the next or previous button on the slider, an error message pops up saying "$curr[action] is not a

I found this interesting Js fiddle that I am currently following: http://jsfiddle.net/ryt3nu1v/10/

This is my current result:

https://i.sstatic.net/VygSy.png

My project involves creating a slider to display different ages from an array, such as 15, 25, 35, 45, 55.

The goal is to show the next age when the "next" button is clicked.

I have implemented the following code snippet from the fiddle to achieve this:

//Age slider
$('div.result-age:gt(0)').hide(); //Hide all but the first one

var $allSlides = $('div.result-age'), 
    traverseDefault = "first", //set the defaults
    actionDefault ="arrow-next";

$('.arrow-next,.selected-arrow-left-pointer').click(function(){

    var traverse = traverseDefault,
        action = actionDefault;

    if($(this).is('.selected-arrow-left-pointer')){ //if action is prev
        traverse = "last"; //set traverse to last in case nothing is available
        action = "selected-arrow-left-pointer"; //set action to prev
    }

    var $curr = $allSlides.filter(':visible'), //get the visible slide
        $nxtTarget =  $curr[action](".result-age"); //get the next target based on the action.

    $curr.stop(true, true).fadeIn(1000).hide(); //hide current one

    if (!$nxtTarget.length){ //if no next
        $nxtTarget = $allSlides[traverse](); //based on traverse pick the next one
    }

    $nxtTarget.stop(true, true).fadeIn(1000); //show the target

});
//age slider end

Here is the HTML structure I am using:

<div class="result-box">
<div class="selected-arrow-left-pointer"></div>
<div class="result-age"><span><h4 v-for="(row,key,index) in ages">ALL ages here currently being display all at once</h4></span></div>
<div class="arrow-next"></div>
</div>

Currently, my design features the age displayed in the center with navigation buttons for previous and next on the sides.

What might I be overlooking in this setup?

Answer №1

It seems like your v-for is generating multiple h4 tags, but you actually need to create a separate result div for each number. To fix this issue, make sure to place your v-for inside the corresponding div tag.

In addition, please note that you are currently using incorrect values for actionDefault and action. The correct values should be next and prev, where next refers to the next slide and prev refers to the previous slide, not the class names as indicated in your code snippet.

Here's a sample code demonstration:

$('div.result-age:gt(0)').hide();
var $allSlides = $('div.result-age'),
  traverseDefault = "first",
  actionDefault = "next"; //use next ..refer next node

$('.arrow-next,.selected-arrow-left-pointer').click(function() {
  var traverse = traverseDefault,
    action = actionDefault;
  if ($(this).is('.selected-arrow-left-pointer')) {
    traverse = "last";
    action = "prev"; //use prev..refer prev..
  }

  var $curr = $allSlides.filter(':visible');
  $nxtTarget = $curr[action](".result-age");

  $curr.stop(true, true).fadeIn(1000).hide();
  if (!$nxtTarget.length) {
    $nxtTarget = $allSlides[traverse]();
  }

  $nxtTarget.stop(true, true).fadeIn(1000);
});
span.next,
span.prev {
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="result-box">
  <div class="selected-arrow-left-pointer">
    << </div>
      <!--your div should have ` v-for="(row,key,index) in ages"`-->
      <div class="result-age"><span><h4>1</h4></span></div>
      <div class="result-age"><span><h4>2</h4></span></div>
      <div class="result-age"><span><h4>3</h4></span></div>
      <div class="arrow-next"> >> </div>
  </div>

Answer №2

The problem has been identified - the incorrect usage of arrow-next instead of next, and selected-arrow-left-pointer instead of prev. Please refer to the corrected snippet below. You have the flexibility to provide data dynamically, although static data has been used in this example.

The keywords next and prev are reserved, causing the error with $curr[action]. In your case, it should have been '$curr['next']' instead of '$curr['arrow-next']', leading to an undefined return value and triggering the error.

//Age slider
      $("div.result-age:gt(0)").hide(); //Hide all but the first one

      var $allSlides = $("div.result-age"),
        traverseDefault = "first", //set the defaults
        actionDefault = "next";

      $(".next,.prev").click(function () {
        var traverse = traverseDefault,
          action = actionDefault;

        if ($(this).is(".prev")) {
          //if action is prev
          traverse = "last"; //set traverse to last in case nothing is available
          action = "prev"; //set action to prev
        }
        var currentData = $allSlides.filter(":visible"), //get the visible slide
          $nxtTarget = currentData[action](".result-age"); //get the next target based on the action.

        currentData.stop(true, true).fadeIn(1000).hide(); //hide current one

        if (!$nxtTarget.length) {
          //if no next
          $nxtTarget = $allSlides[traverse](); //based on traverse pick the next one
        }

        $nxtTarget.stop(true, true).fadeIn(1000); //show the target
      });
.next, .prev {
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="result-box">
      <div class="prev"><</div>
      <div class="result-age">
        <span><h4>ALL ages here currently being display all at once</h4></span>
      </div>
      <div class="result-age">
        <span><h4>2</h4></span>
      </div>
      <div class="result-age">
        <span><h4>3</h4></span>
      </div>
      <div class="result-age">
        <span><h4>4</h4></span>
      </div>
      <div class="result-age">
        <span><h4>5</h4></span>
      </div>
      <div class="next">></div>
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

javascript if an error occurs, refresh the webpage

Currently, I am inquiring about the most effective method for managing JavaScript errors. Given that my application relies heavily on JavaScript, despite diligent testing efforts, encountering bugs is almost certain. I'm interested in knowing if ther ...

Initiate Child Event within Parent Component

Before switching tabs in the parent component, I want the child tab to validate itself. My idea is to pass the onActive event from the parent to its children, <ClientInfo/> and <Details/>. This will allow the children to validate themselves a ...

How to use Ionic 3 to automatically scroll ion-scroll content all the way to the bottom

My ion-scroll component is experiencing some challenges <ion-scroll scrollY="true" style="height: 52vh;"> {{ text }} </ion-scroll> The content inside the ion-scroll keeps expanding, exceeding the designated height. Users can manually scroll ...

The success function within the Ajax code is malfunctioning

I am currently utilizing express, node.js, and MySQL. The issue I am facing is that the success function inside my Ajax code is not working as expected. Below is the snippet of the Ajax code in question: function GetData_1(){ var state = $("#dpState_1"). ...

Using ASP.NET to cleverly include script files based on certain conditions

We are currently developing a CMS project in ASP.NET, utilizing jQuery for our client-side scripting needs. At the moment, our project includes the jquery-1.2.6.js file as a mandatory script file. Additional script files are loaded as needed, based on the ...

Tips for picking out a particular item from a list of child elements

When I select the first parent's children array, it ends up selecting every other parent's children as well. This is due to index 0 remaining the same for all of them. How can I specify and highlight a specific child? Link: Check out the stackb ...

Monitoring user clicks using JavaScript code implemented

Currently, I am utilizing a JavaScript file to load on certain pages in order to track various events. Within this JavaScript file, there is a collection of selectors that have listeners attached to them. When these selectors are clicked, the tracking AP ...

Managing "unprocessed information" in a Node.js environment and transferring the information through a Node Express endpoint

Currently, I am in the process of making an API call to retrieve a file using axios: async function fetchData() { const configuration = {}; // { responseType: 'stream'}; const { response } = await axios.get(URL, configuration); c ...

Issue with React Toggle functionality on mobile devices

I have a hamburger toggle that I found in a template, but it doesn't seem to be functioning correctly. How can I activate the on/off toggle feature? When I click the toggle, nothing happens. What am I missing? <div data-testid="header"> ...

The functionality of images and links is compromised when they are assigned as values to properties within a JSON object

My images and links are not working, even after declaring them globally. When I write the src directly into src, everything seems fine but the alert pops up with the same URL and img src. Can anyone help? var combo0; var combo1; var combo2; var combo3; ...

Errors encountered by the Chrome extension

Hey there, I've recently delved into creating a chrome extension but hit a roadblock. My issue revolves around the service worker registration failing and encountering errors related to undefined properties. https://i.stack.imgur.com/bGzB4.png The c ...

What is the process of including data in Vue using refs?

My goal is to click a button and have the page scroll up or down. However, I need references for each span I add in order to include an index. The issue arises when trying to incorporate this index into the method. How can I add data to these references? ...

How to associate an object with a component in Angular2/TypeScript using HTTP

I am currently working on displaying a list of item names retrieved from a REST API. By utilizing the map function on the response observable and subscribing to it, I was able to obtain the parsed items object. Now, my challenge is how to bind this object ...

During an AJAX call in MVC, the DDL remains unchanged

I am looking to update the value of location dynamically during an ajax call, in order to save the user time from manually changing it. I have attempted a solution but it doesn't seem to be working as expected. Any ideas on where I might be going wron ...

What impact does the text-shadow property have on altering the color of hsla text?

I created a header that changes color to be slightly transparent with a black outline when hovered over (achieved using 4 text-shadows). However, I encountered an issue when trying to use hsla() to define the color. The color defaults to black with 100% o ...

The Spring Boot REST application is unexpectedly returning HTML instead of JSON, causing confusion for the Angular application which is unable to recognize the

I am struggling with my Spring Boot application that was generated using . I am having difficulty getting it to return JSON instead of HTML. Here is a snippet of the code: [@CrossOrigin(origins="http://localhost:4200",maxAge=3600) @RestController @Request ...

Creating a unique function to map an array in VueJS specifically designed for table manipulation

I am currently working on displaying and sorting data in a bootstrap table within VueJS. My goal is to change the date format within an array retrieved from an API endpoint. The original date format is in "January 21, 2010" and I need it to be in "MM/DD/Y ...

Creating a dynamic table accordion with PHP and MySQL

I am currently working on creating an accordion table to display data retrieved from a database. I want the description data to be shown in a row below when clicking on the respective row. Despite my efforts in modifying various code snippets that I have c ...

The JavaScript alert message pops up two times consecutively

I encountered an issue while attempting to utilize a module named disclaimer in Drupal 7. The alert message "You must enter the year you were born in." appears twice and then redirects to a URL that should only be accessed after verifying that you are over ...

What is the best way to share models across different node.js projects?

In my setup, I have two node.js projects - project A and project B. Project A serves as the main project, while project B is more of an "ad-hoc" project with a specific purpose. The challenge lies in the fact that project B requires access to project A&apo ...