?
Solved

Jquery each function onclick problem

Posted on 2016-07-29
2
Medium Priority
?
69 Views
Last Modified: 2016-07-29
Hello experts.
I need help with my js code.
I have div s with the same class mainlist , all are hidden except the first one.
I try to hide each  div and show the next one with the same class on button click.
I m trying to do this with each function to find the no hidden div but it is not working.
Any help?

$('form#Form1').on('click', 'button.nextstep', function() {
    $('div[class="mainlist"]').each(function(i, obj) {
        if (obj.css('display') !== 'none') {
            var tmp1 = obj;
        }
    });
    var tmp2 = tmp1.next('div.mainlist');
    tmp1.hide();
    tmp2.show();
});
0
Comment
Question by:Panos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 2000 total points
ID: 41735216
You can do it very simple.
Something like this:
$('form#Form1').on('click', 'button.nextstep', function() {
	
  var current = $('div.mainlist:visible') || $('div.mainlist').first();
  current.hide().next().show();
  return false;
});

Open in new window

Of course, you still need to validate is you're at the last one already or something like that, but as per your question, this works.

Heres a working sample
0
 
LVL 2

Author Comment

by:Panos
ID: 41735230
Hi Alexandre Simões
Thank you for your help. The code is doing the job.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question