0

I am quite new to jquery and was wondering if someone could recommend some ways I could combine these functions together so that the code would be more efficient and slimmed down. I basically have two click functions that do the exact same thing but are targeting two different buttons. Thank you.

        $( ".step1 button" ).click(function() {     
            if ($(".step1 input").is(":checked")) { 
                $( ".step1").slideUp("slow",function(){
                    $(".step2").slideDown("slow");
                });
            } // end if
            else{
                $("div.error").remove();
                $(".step1.step-heading").append("<div class='error'><i class='fa fa-exclamation-triangle'></i> Please make a selection before continuing</div>");
            } // end else         
        })// end click function

        $( ".step2 button" ).click(function() {     
            if ($(".step2 input").is(":checked")) { 
                $( ".step2").slideUp("slow",function(){
                    $(".step3").slideDown("slow");
                });
            } // end if
            else{
                $("div.error").remove();
                $(".step2.step-heading").append("<div class='error'><i class='fa fa-exclamation-triangle'></i> Please make a selection before continuing</div>");
            } // end else         
        })// end click function
1

2 Answers 2

1

You can access the DOM element tha trigger the event by using $(this) inside the vent handler.

So, you simply have to attach the same handler to several elements, and use $(this) inside the implementation of the handler.

$(".step1 button").add(".step2 button").click(function() {
    // $(this) refered to the button tha triggered the event
}

To make easier to find the step number you can use a custom "data-" attribute, for example like this:

<button data-step-no="1">

You can get that attribute by using $(this).attr('data-step-no'), In fact, to add or substract, you need to parse it like this: var stepno = parseInt($(this).attr('data-step-no').

To select all the buttons wit a single selector you can use buttons like this:

<button data-step-no="1" class="step">
<button data-step-no="2" class="step">

Ans select them all in a single step:

$('button .step').click(...
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a simple refactor that will not require changes to HTML:

$('[class^="step"]').each(function(k, v) {
    var $btn       = $(v).find('button'),
        $input     = $(v).find('input'),
        $next      = $(v).next(),
        $heading   = $(v).find('.step-heading'),
        error_html = "<div class='error'><i class='fa fa-exclamation-triangle'></i> Please make a selection before continuing</div>";

    $btn.click(function(e) {
        if ($input.is(':checked')) {
            $(v).slideUp('slow', function() {
                $next.slideDown('slow');
            });
        } else {
            $('div.error').remove();
            $heading.append(error_html);
        }
    });
});

Comments

Your Answer

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.