jquery click event not triggering on generated element

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
Alex MacAsked:
Who is Participating?
 
Julian HansenCommented:
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
 
Brian TaoSenior Business Solutions ConsultantCommented:
  • 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
 
Alex MacAuthor Commented:
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
 
Julian HansenCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.