Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your click handler needs two things: preventDefault(), and an AJAX method such as jQuery's $.ajax() or $.get().</p> <p>preventDefault() will prevent the default action of navigating the page to the href value. Add a parameter to the click event to represent the event, and call preventDefault() on that.</p> <p>$.ajax() or $.get() will perform the action of hitting the URL to cast the vote. Personally I prefer $.ajax(), it performs a GET request by default but you can set the type to POST. There are many other options available such as dataType (json, text, xml, etc.). The success and error functions are straight-forward to implement too.</p> <p>JavaScript:</p> <pre><code>$('ul.list1 li a').click(function(e) { e.preventDefault(); // cache this for scope change var $this = $(this); $.ajax({ url: this.href, success: function() { // your original JavaScript here $('a.button1').removeClass('active'); $this.addClass('active'); // no need for .parent().find() $('div#content div.comments').fadeIn('slow'); var col1Height = $("div#left-pannel").height() - 63; $('div#sidebar').css("min-height", col1Height); }, error: function() { alert('Sorry, your vote was not successful.'); } }); }); </code></pre> <p>I also changed how you called addClass() in your code. First, we needed to cache $(this) since "this" will no longer refer to the clicked element in the success function. Second, based on the HTML you provided, the element you need to find, 'a.button', <em>is</em> the element that was clicked. If that is the case, we can drop .parent().find('a.button') and just operate on $this directly.</p> <p>More on jQuery.ajax(): <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">http://api.jquery.com/jQuery.ajax/</a></p> <p>Feel free to ask questions about the code. I hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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