0

I'm working with jquery and this code is working but I wanto to improve it because I'm not re-using code. Theres a way to dinamically sent $("#btn_step1") changing the number oh the step?

$( document ).ready(function() {
 var activeDiv = $("#mod_formSteps-1");
 var body = $("html, body");
 activeDiv.siblings().hide();
 $("#btn_step1").on("click", function( event ){
 event.preventDefault();
 activeDiv.hide();
 activeDiv.next().show();
 activeDiv = $("#mod_formSteps-2");
 body.animate({scrollTop:0}, '500', 'swing');
 });
 $("#btn_step2").on("click", function( event ){
 event.preventDefault();
 activeDiv.hide();
 activeDiv.next().show();
 activeDiv = $("#mod_formSteps-3");
 body.animate({scrollTop:0}, '500', 'swing');
 });
 $("#btn_step3").on("click", function( event ){
 event.preventDefault();
 activeDiv.hide();
 activeDiv.next().show();
 activeDiv = $("#mod_formSteps-4");
 body.animate({scrollTop:0}, '500', 'swing');
 });
});
asked Aug 15, 2014 at 22:35

4 Answers 4

1

You can do something like this using the attribute starts-with selector

// get all elements with id's that start with btn_step
$("[id^='btn_step']").on("click", function( event ){ 
 activeDiv.hide();
 activeDiv.next().show();
 activeDiv = $("#mod_formSteps-" + (+this.id.replace('btn_step','') + 1));
 body.animate({scrollTop:0}, '500', 'swing');
});

or give them a similar class

$(".theClass").on("click", function( event ){ 
 activeDiv.hide();
 activeDiv.next().show();
 activeDiv = $("#mod_formSteps-" + (+this.id.replace('btn_step','') + 1));
 body.animate({scrollTop:0}, '500', 'swing');
});

Looking at the code more you can actually just do this

$(".theClass").on("click", function( event ){
 activeDiv = activeDiv.hide().next().show(); 
 body.animate({scrollTop:0}, '500', 'swing');
});
answered Aug 15, 2014 at 23:03
Sign up to request clarification or add additional context in comments.

Comments

0

You could use classes for the btn_stepsinstead of individual IDs. Using classes you could use one single function that is fires once a .btn_step element is clicked.

answered Aug 15, 2014 at 22:38

Comments

0

Only thing that's changing is a number so something like

$( document ).ready(function() {
 var activeDiv = $("#mod_formSteps-1"),
 body = $("html, body"),
 steps = 4,
 i = 0;
 activeDiv.siblings().hide();
 for (i; i < steps; i++) {
 var selector = "#btn_step" + i;
 $(selector).on("click", function( event ){
 event.preventDefault();
 activeDiv.hide();
 activeDiv.next().show();
 activeDiv = $("#mod_formSteps-" + (i + 1));
 body.animate({scrollTop:0}, '500', 'swing');
 })
 }
});
answered Aug 15, 2014 at 22:43

Comments

0

You have lots of code duplication, given that you are performing the same operation on elements with three different id attributes. Consider adding a class btn_step to each of the elements btn_step1, btn_step2, and btn_step3. Then, keep track of what step you are on when assigning the click function to each element of the btn_step class:

$( document ).ready(function() {
 var body = $("html, body");
 $(".btn_step").each(function(index, element) {
 element.click(function() {
 var step = index + 1;
 event.preventDefault();
 var activeDiv = $("#mod_formSteps-" + step);
 activeDiv.siblings().hide();
 activeDiv.hide();
 activeDiv.next().show();
 activeDiv = $("#mod_formSteps-2");
 body.animate({scrollTop:0}, '500', 'swing');
 });
 });
};
answered Aug 15, 2014 at 22:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.