Solved

jquery click event not triggering on generated element

Posted on 2016-09-29
4
58 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
[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
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 56

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 56

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)

749 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