1

I know this is simple but I cant get the conditional if below to work...

This line below " if ( !$section.id == "no-transition" ) " is not correct.

I'm trying to stop javascript from working on section elements that have the id of "no-transition".

Can someone point me in the right direction?

function initEvents() {

    $sections.each( function() {

            var $section = $( this );

            if ( !$section.id == "no-transition" ) { 
            // expand the clicked section and scale down the others
            $section.on( 'click', function() {

                    [code taken out to shorten post...]

                    if( !supportTransitions ) {
                            $section.removeClass( 'bl-expand-top' );
                    }

                    $el.removeClass( 'bl-expand-item' );

                    return false;

            } );
           }
} );
5
  • 6
    You really should not have multiple elements with the same id on a page. Its baad! Commented May 4, 2013 at 14:00
  • 1
    This is why you should always use != and never !... ==. :) Commented May 4, 2013 at 14:02
  • You have syntax error in your code. Where is the } for the function? Commented May 4, 2013 at 14:03
  • To be more clear about why you should not have the same id for multiple elements: the id attribute is not built to have the same value assigned to multiple elements. If you attempt to .getElementById or use some kind of jQuery event handler like $('#no-transition').click(... it will only return/be applied to the first element. If you want to act on multiple elements, use a shared class attribute value. Commented May 4, 2013 at 14:13
  • if I changed it to a class what would the corresponding conditional if be? if ($section.attr('class') != "no-transition") ? Commented May 4, 2013 at 14:22

1 Answer 1

3

You really should not have multiple elements with the same id on a page.

Your if condition should be modified to:

...
if ( $section.attr('id') != "no-transition" ) { 
   ...
Sign up to request clarification or add additional context in comments.

1 Comment

Superb techfoobar! I apologize I didn't have enough javascript experience to get this one right. Thanks for everyone's 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.