• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 86
  • Last Modified:

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
0
Alex Mac
Asked:
Alex Mac
  • 2
2 Solutions
 
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
 
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
 
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

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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now