1

With my fiddle, I made it to highlight on one of main menu buttons when loaded and show() and hide() corresponding button set in the bottom.

What I think is wrong but can't figure out myself is something to do $this variable's reference scope in my switch statement. In my case, $this points to the clicked button value which I presume is the name of the class but in my switch statement, I am not sure whether I can reference the id names from $this. I guess my question is what value does $this hold in my scenario?

It would be nice to see a working example, possibly with less code as I can see many duplicates where it could be simplified.

$('.menu').click(function () {
    if ($(this) != $('.highlight')) {
        $(this).addClass('highlight')
            .siblings('.menu')
            .removeClass('highlight');
    }
    switch ($this) {
        case '#dateMenu':
            $('.date-chart').show();
            $('.jc-chart').hide();
            $('.jp-chart').hide();
            $('.ws-chart').hide();
            break;
        case '#jcMenu':
            $('.date-chart').hide();
            $('.jc-chart').show();
            $('.jp-chart').hide();
            $('.ws-chart').hide();
            break;
        case '#jpMenu':
            $('.date-chart').hide();
            $('.jc-chart').hide();
            $('.jp-chart').show();
            $('.ws-chart').hide();
            break;
        case '#wsMenu':
            $('.date-chart').hide();
            $('.jc-chart').hide();
            $('.jp-chart').hide();
            $('.ws-chart').show();
            break;
    }
});
9
  • 1
    I would have thought:switch (this.id) ... case ("wsMenu") - but you likely just need to hide the siblings and show $this once you set $this=$(this) - and you need $(this).hasClass('.highlight') Commented Jul 15, 2013 at 4:45
  • put a debugger; in your code and open up your console in firefox or chrome. console.log(this) Commented Jul 15, 2013 at 4:45
  • 1
    where is $this ?? i guess that should be $(this) which again makes no sense.. :) Commented Jul 15, 2013 at 4:47
  • That is not an answer Commented Jul 15, 2013 at 4:50
  • rather you should put that in comments. Commented Jul 15, 2013 at 4:51

2 Answers 2

2

$this should be $(this) ... this is the refrence to the current element... since you are checking the id. use prop() .

 switch ($(this).prop('id')) {  or  switch (this.id) { ...

and remove the # form the case

try this

 $('.menu').click(function () {
if (!$(this).hasClass('.highlight'))) {
    $(this).addClass('highlight')
        .siblings('.menu')
        .removeClass('highlight');
}

switch ($(this).prop('id')) {
    case 'dateMenu':
        $('.date-chart').show();
        $('.jc-chart').hide();
        $('.jp-chart').hide();
        $('.ws-chart').hide();
        break;
    case 'jcMenu':
        $('.date-chart').hide();
        $('.jc-chart').show();
        $('.jp-chart').hide();
        $('.ws-chart').hide();
        break;
    case 'jpMenu':
        $('.date-chart').hide();
        $('.jc-chart').hide();
        $('.jp-chart').show();
        $('.ws-chart').hide();
        break;
    case 'wsMenu':
        $('.date-chart').hide();
        $('.jc-chart').hide();
        $('.jp-chart').hide();
        $('.ws-chart').show();
        break;
}
});

fiddle here

updated

for reduced code... you can use siblings().

 switch ($(this).prop('id')) {
    case 'dateMenu':
        $('.date-chart').show().siblings().hide();

        break;
    case 'jcMenu':

        $('.jc-chart').show().siblings().hide();

        break;
    case 'jpMenu':

        $('.jp-chart').show().siblings().hide();

        break;
    case 'wsMenu':

        $('.ws-chart').show().siblings().hide();
        break;
}

updated fiddle

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

1 Comment

+1 but please fix the erronous if ($(this) != $('.highlight')) which I believe should be if (!$(this.hasClass('.highlight'))
1

($this) is wrong without assigning, instead use this.id

You can use indeed like

var id = this.id;
switch (id)

And remove # in your cases and make like below

switch (this.id) {
        case 'dateMenu': //removed #
            $('.date-chart').show();
            $('.jc-chart').hide();
            $('.jp-chart').hide();
            $('.ws-chart').hide();
            break;
        case 'jcMenu':  //removed #
            $('.date-chart').hide();
            $('.jc-chart').show();
            $('.jp-chart').hide();
            $('.ws-chart').hide();
            break;
        case 'jpMenu':  //removed #
            $('.date-chart').hide();
            $('.jc-chart').hide();
            $('.jp-chart').show();
            $('.ws-chart').hide();
            break; 
        case 'wsMenu':  //removed #
            $('.date-chart').hide();
            $('.jc-chart').hide();
            $('.jp-chart').hide();
            $('.ws-chart').show();
            break;
    }

Working fiddle

4 Comments

$this=this.id is poor documentation. $x normally signifies that the var is a jQuery object.
@mplungjan Thanks for pointing the mistake. From googling, james.padolsey.com/javascript/jquery-code-smells I came to know it is an ugly practice. But anyother reason?
IF you use $this, THEN make sure it is $(this). IF you use $mydivs THEN make sure it is $("some selector returning divs") - if your code is to be self documenting do NOT use a var that has a name that confuses a reader. If you do not want to use $ then that is fine. It would still be id = this.id or in this case no need to save it at all since we only use it once.
You are welcome. PS: With 2.5K rep, you should update your profile with name/avatar :)

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.