0

Here's the current jQuery code I'm trying to use

$("ul.nav.nav-tabs li.active a").each(function(){
        switch(this.hash) {
            case "#core":
                alert("Core");
            break;
            case "#parallel":
                alert("Parallel");
            break;
            case "#cron_settings":
                alert("Cron Settings");
            break;  
        }
})

Here's the HTML Markup I'm using also

<ul class="nav nav-tabs">
    <li class="active"><a href="#core" data-toggle="tab">Core Settings</a></li>
    <li><a href="#parallel" data-toggle="tab">Parallel settings</a></li>
    <li><a href="#cron_settings" data-toggle="tab">Cron limit settings</a></li>
</ul>

When the active class changes, I need it to basically re-run the switch() as it's only being performed once on the document.ready, how can I get it to re-run the switch()? possibly using change() ?

Michel
  • 26,600
  • 6
  • 64
  • 69
Curtis
  • 2,646
  • 6
  • 29
  • 53
  • There is no cross-browser event for element attribute changes – Bergi Dec 24 '13 at 21:05
  • Depending on what browser you are testing, you might also find interesting the`DOMAttrModified` event. Keep in mind it's not supported by all browsers. See http://help.dottoro.com/ljfvvdnm.php#additionalEvents for details. – Thalis K. Dec 24 '13 at 22:35

3 Answers3

2

Based on earlier poster who suggested the 'hashchange' event, here is a working copy of what you want:

$('li').on('click', function() { 
    $('li').removeClass('active');
    $(this).addClass('active'); 
    // UNNECESSARY: window.location.hash=$('a', this).attr('href'); 
});

$(window).on('hashchange', function(){
        switch(window.location.hash) {
            case "#core":
                alert("Core");
                break;
            case "#parallel":
                alert("Parallel");
                break;
            case "#cron_settings":
                alert("Cron Settings");
                break;
        }
}); 
Thalis K.
  • 7,363
  • 6
  • 39
  • 54
  • That's nifty. But it only works if the links are clicked. Since that's true, why don't you use the on click callback to run the switch? – Omn Dec 24 '13 at 23:23