Solved

jquery click event not triggering on generated element

Posted on 2016-09-29
4
35 Views
Last Modified: 2016-09-30
Hello,
I'm trying to get this form to show and hide different sections when next and previous are clicked using jQuery.

The next button works, however on the second section when the previous button is added to the form, nothing happens when you click on the previous button.

The link to the page is below:

http://www.quotechief.com/testquote/

Thanks
0
Comment
Question by:Alex Mac
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:Brian Tao
Brian Tao earned 150 total points
Comment Utility
  • Because the "next" button exists before you bind its click event, but "previous" button doesn't.  It's dynamically created, so the event binding must take such form as:
$(staticAncestors).on(eventName, dynamicChild, function() {});

Open in new window

and in you page, probably:
$('#form_container').on("click","#previous", function() {});

Open in new window

  • jQuery won't understand the "slide_count" in the following line. Remove "slide_count" from it:
$('#previous').on("click",function(event,slide_count){...});

Open in new window

1
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 350 total points
Comment Utility
Basic wizard functionality
CSS
<style type="text/css">
section.wizard {
  display: none;
}
section.wizard:first-child {
  display: block;
}
.btn-prev, .btn-done {
  display: none;
}
</style>

Open in new window

HTML (bootstrap)
    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <section class="wizard">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3 class="panel-title">This is section 1</h3>
            </div>
            <div class="panel-body">
              <p>The content for section 1 goes here</p>
            </div>
          </div>
        </section>
        <section class="wizard">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3 class="panel-title">This is section 2</h3>
            </div>
            <div class="panel-body">
              <p>The content for section 2 goes here</p>
            </div>
          </div>
        </section>
        <section class="wizard">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3 class="panel-title">This is section 3</h3>
            </div>
            <div class="panel-body">
              <p>The content for section 3 goes here</p>
            </div>
          </div>
        </section>
        <button class="btn btn-warning btn-nav btn-prev">Previous</button> 
        <button class="btn btn-primary btn-nav btn-next pull-right">Next</button>
        <button class="btn btn-primary btn-nav btn-done pull-right">Finish</button>
      </div>
    </div>

Open in new window

jQuery
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$(function() {
  var sections = $('section.wizard').length;
  var next = $('.btn-next');
  var prev = $('.btn-prev');
  var done = $('.btn-done');
  var selected = 0;
  
  $('.btn-nav').click(function(e) {
    var action = $(this).data('action');
    $('section.wizard').hide();
    selected += 1*action;
    selected = selected % sections;
    $('section.wizard:eq(' + selected + ')').show();
    if (selected > 0) {
      prev.show();
      next.show();
      done.hide();
    }
    if (selected == 0) prev.hide();
    if (selected == sections - 1) {
      next.hide();
      done.show();
    }
  });
});
</script>

Open in new window

Working sample here
1
 

Author Comment

by:Alex Mac
Comment Utility
Thanks, great help once again.

What would be the best way of adding a slide animation (from left and right) for the different sections?
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are most welcome.

To slide left - there are a few approaches.
Easiest is probably just to put the sections in a carousel and make the next and previous buttons the Nav buttons for the carousel.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now