Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd refer to the comments on the question - if I understand your intent (click a link, update something behind the scenes on the server, and apply a style on the page without reloading the page) this is probably something you'd want to do with AJAX. For example (untested code):</p> <pre><code>$('.unanswered, .answered').click(function() { $.ajax({ url: $(this).attr('href') }).done(function() { /* You'll need to add some logic in here to remove the active class from the answered or unanswered question if it exists. Depending on the rest of your HTML, this could be done with something like $(this).closest('myWrapper').find('a.active').removeClass('active') */ /* Once you've cleaned up any active classes, add an active class to this element */ $(this).addClass('active'); }); }); </code></pre> <p>More reading on jQuery AJAX here: <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">http://api.jquery.com/jQuery.ajax/</a></p> <p>Since I don't understand exactly what you're attempting to do, you can also try this (which will update the class, then submit the request). You may not see the CSS changes reflected since the page will reload quickly.</p> <pre><code>$('.answered').click(function() { // Set up the classes here $('.unanswered a').removeClass('active'); $(this).addClass('active'); /* When this element has an active class, then it will redirect to the link's URL, the HREF attribute. We do this check because javascript is asynchronous, all lines can run at the same time, so this prevents window.location from being called before the class is changed */ if($(this).hasClass('active')) { window.location = $(this).attr('href'); } // Still return false to prevent the default redirect that would happen without javascript. return false; }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload