6

propb. very simple, but not for me. Looking to pass a variable on a click function to show either div a or div b based on the a link clicked. My code is something like this

$('.view').click(function() {
    var id = this.id.replace('view_', "");
if(id=1) { $('#show').show('slow'); }
if(id=2 ) { $('#show2').show('slow'); }
$('#categories').hide('slow');
    return false;
  });
  }); 

But OBVIOUSLY the if statements are wrong - I know that I am just using them as an example. Any suggerstions? Thanks in adavance

1
  • 1
    It looks like you found @naeem's answer helpful. Don't forget to mark it as accepted if that is the case. I see you are new here, so I wanted to make sure you knew how important it is to accept answers on every question you ask if possible. And welcome to Stack Overflow! Commented Jan 15, 2010 at 8:36

4 Answers 4

6

You are assigning the value of 1 to id instead of testing for a match:

if(id = 1) {} // WRONG: this means if id is successfully SET to 1

Here is what it should look like:

$('.view').click(function() {
    var id = this.id.replace('view_', "");

    if(id == 1)       { $('#show').show('slow'); }
    else if(id == 2 ) { $('#show2').show('slow'); }

    $('#categories').hide('slow');

    return false;
});

If you find yourself making this mistake a lot, you should switch the test around:

if( 1 == id) {} // Works
if( 1 = id ) {} // Throws JS error instead of failing silently
Sign up to request clarification or add additional context in comments.

2 Comments

+1 this post finally reveals why people write (1==id) instead of the other way round
Well explained, +1 for that
4

try this...

var id = parseInt(this.id.replace('view_', ""), 10);
if(id === 1) { 
 $('#show').show('slow'); 
} else if(id === 2 ) { 
 $('#show2').show('slow'); 
}

1 '=' is used for assignment
2 '==' is comparison with type conversion
3 '===' is comparison without type conversion

1 Comment

Perfect Naeem, thanks, works stright out of box. I am just learning JQuery/java so some of the lang is a bit strange for me as a PHP guy Thanks again
0

If your link id's are view_1 and view_2, then all should be fine here, except for the fact that you're using = rather than comparison ==.

If your show elements were called show1 and show2, rather than show and show2, you could of course do:

$('.view').click(function() {
    $('#show' + this.id.replace('view_','')).show('slow');
    $('#categories').hide('slow');
    return false;
});

Comments

0

As Doug pointed out, the problem is in conditional assignment

You should also consider renaming your ids to directly match your view ids, so you can do it without test cases, only by string concatenation:'

$('.view').click(function() {
    var id = this.id.replace('view_', "");

    $('#show' + id).show('slow'); // doesn't have to use if statements

    $('#categories').hide('slow');

    return false;
});

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.