Results 1 to 8 of 8

Thread: addClass Takes Two Clicks To Function

  1. #1
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default addClass Takes Two Clicks To Function

    I'm using "Coda-Slider 2.0" for a tab-based interface. I used external triggers to switch between tabs, and I'm having trouble adding my "selected" class when I click on one of the triggers.

    It works...BUT...it takes two clicks for it to respond correctly. The slider works just fine, but the "selected" class isn't added (i.e. the selected tab gets a different background image) until I click on one of the menu items twice. Here is the JavaScript I used:

    Code:
    $("a").click(function(){
        $(".menu a.selected").removeClass("selected");   // remove previous class if there is any
        $(this).addClass("selected");                             // add class to the clicked link
        return false;                                                              // prevents browser from following clicked link
    });
    And here is the HTML. The "xtrig" class is required for it to be used as an external trigger for the slider.

    Code:
    <ul class="menu">
    		<li class="day1"><a class="selected xtrig" href="#1" rel="coda-slider-1">Day 1</a></li>
    		<li class="day2"><a class="xtrig" href="#2" rel="coda-slider-1">Day 2</a></li>
    		<li class="day3"><a class="xtrig" href="#3" rel="coda-slider-1">Day 3</a></li>
                     <li class="day4"><a class="xtrig" href="#4" rel="coda-slider-1">Day 4</a></li>
    </ul>
    Any ideas why it would take two clicks for it to function? Thanks!
    Last edited by georgiadogs; 01-27-2010 at 03:10 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Please post a link to the page on your site that contains the problematic script so we can check it out.
    Jeremy | jfein.net

  3. #3
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Whoops, here ya go---If you click on "Day 2," the slider will activate, but that tab won't be highlighted. However, if you click on it again, the tab will be highlighted.

    http://nlloyd.zxq.net/coda-slider/

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Remove the highlighted:
    Code:
    $("a").click(function(){
        $(".menu a.selected").removeClass("selected");   // remove previous class if there is any
        $(this).addClass("selected");                             // add class to the clicked link
        return false;                                                              // prevents browser from following clicked link
    });
    And then at the bottom of the bind function, add:
    Code:
    return false;
    Here you can view the changed javascript: http://unlinkthis.net/examples/jquery.code.js
    Jeremy | jfein.net

  5. #5
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Perfect, thanks! Can you give me a basic reason why that worked?

    Thanks again for the help.

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    It worked because the bind function was already calling the onclick function. Its basically saying:
    Code:
    When you press a button do this:
      When you press the button again do this:
        Give the button a class
    I don't know if that made sence - if it didn't I'd be happy to explain it differently.
    Jeremy | jfein.net

  7. #7
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    No I think I got it..it was weird adding in my own code to someone else's (considering I don't have much of a grasp on JS or jQuery). Thanks again!

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help you! Your welcome!

    It seems your topic is solved... Please set the status to resolved.. To do this:
    Go to your first post ->
    Edit your first post ->
    Click "Go Advanced" ->
    Then in the drop down next to the title, select "RESOLVED"
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •