0

I am very new to OOP. my understanding is that, it is like CSS class, we have classes then we could apply/use it.

this is my code.

$(".mybutton").click(function(){
    $(this).siblings('.mybutton').removeClass('active').end().addClass('active');           
});


$('.mybutton, .second').click(function(event){
    $('#thisDiv').hide().filter(this.hash).show();
    event.preventDefault();
});

$('.second').on("click", function(){
$(".mybutton").eq(1).siblings('.mybutton').removeClass('active').end().addClass('active');
    $("html, body").animate({
        scrollTop: $('#contact').offset().left - 20},
        800);
});

how should I convert them to oop style?

var Doit = {
    init:function(){
        $(".mybutton").on("click", this.active);
    },
    active:function(){
    $(this)
      .siblings('.mybutton')
          .removeClass('active')
              .end().addClass('active');
    },
    filter:function {


    }


};

Doit.init();

if someone could give me a hint, if my code really needs to be changed to OOP ?

Thanks

1
  • 1
    Check out the jqueryboilerplate.com. That's a way of doing it. Commented Jun 5, 2013 at 4:56

1 Answer 1

1

Try it like,

var Doit = function(){
    this.init=function(){
        $(".mybutton").on("click", this.active);
    },
    this.active=function(){
        $(this)
          .siblings('.mybutton')
              .removeClass('active')
                  .end().addClass('active');
    },
    this.filter=function() {
        //code
    }
};

Read More Javascript Object Oriented

Sign up to request clarification or add additional context in comments.

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.