Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7845685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:19:21+00:00 2026-06-02T17:19:21+00:00

I admittedly am not very proficient with Javascript (and subsequently AJAX). I have a

  • 0

I admittedly am not very proficient with Javascript (and subsequently AJAX). I have a button that you click to vote on a page. Once clicked, the page drops and expands the section so that you can see the comments below it (so that the voter will not be swayed by the comments). This works beautifully, but unfortunately, clicking the button does not actually load the PHP controller to submit the vote to the database.

View:

<div class="vote clearfix">
    <ul class="list1 clearfix">
        <li class="css3">
            <a href="<?=base_url()?>vote/submit_vote/<?=$post?>/1/1" class="button1 css3">
            <span>Button Text</span></a>
        </li>
    </ul>
</div>

JavaScript:

$('ul.list1 li a').click(function() {
    $('a.button1').removeClass("active");
    $(this).parent().find('a.button1').addClass("active");
    $("div#content div.comments").fadeIn('slow');
    var col1Height = $("div#left-pannel").height() -63 ;
    $('div#sidebar').css("min-height", col1Height );
    return false;
});

I would like this button to submit the vote to a controller. What would be the best way to fix the button so that it links normally?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:19:22+00:00Added an answer on June 2, 2026 at 5:19 pm

    Your click handler needs two things: preventDefault(), and an AJAX method such as jQuery’s $.ajax() or $.get().

    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.

    $.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.

    JavaScript:

    $('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.');
            }
        });
    });
    

    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’, is the element that was clicked. If that is the case, we can drop .parent().find(‘a.button’) and just operate on $this directly.

    More on jQuery.ajax(): http://api.jquery.com/jQuery.ajax/

    Feel free to ask questions about the code. I hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I'm about finished with my latest project, a (admittedly not very good)
SQL/PHP query works in PHPmyAdmin but not the site. I notice that many have
Admittedly I don't get it. Say you have a memory with a memory word
This is admittedly a rather loose question. My current understanding of singletons is that
I have a big mess of code. Admittedly, I wrote it myself - a
I'd like to have access to the bytecode that is currently running or about
Let me start by saying that I have been reading the drag'n drop tutorial
I have limited screen space for a table that I'm displaying, so I'm showing
I have the following jQuery (admittedly messy at the moment) which appends a new
EDIT NOTE: I've not been very clear. I'm trying to start from a single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.