Solved

jquery click event not triggering on generated element

Posted on 2016-09-29
4
48 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
ID: 41821655
  • 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 54

Accepted Solution

by:
Julian Hansen earned 350 total points
ID: 41821750
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
ID: 41822554
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 54

Expert Comment

by:Julian Hansen
ID: 41823250
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)

776 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