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

  • Home
  • SEARCH
  • 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 6149789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:30:47+00:00 2026-05-23T19:30:47+00:00

I’ve just got a jquery function working with my rails3 application. When a user

  • 0

I’ve just got a jquery function working with my rails3 application. When a user clicks a checkbox on the page, it toggles the status and fades out:

 $('#complete_task_1').click(function() {
  $.ajax({
    url: $(this).attr('href'),
    type: 'get',
    dataType: 'html',
    success: function(data, textStatus, jqXHR) {
        $('#complete_task_1').closest('tr').fadeOut();
    },
    error: function(data, status, error){
        alert('Oooops!');
      }
  });
});

Initially I wanted to use $(this) instead of $(‘#complete_task_1’) but I couldn’t get that working. Right now, it’s all working fine.

My problem is that I need to reload the page manually after checking one box – otherwise, when I click another items checkbox, the js doesn’t seem to be called. It’s neither a success or error. There’s nothing in the dev log either.

Is there an easy way around this??

  • 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-05-23T19:30:47+00:00Added an answer on May 23, 2026 at 7:30 pm

    Initially I wanted to use $(this) instead of $(‘#complete_task_1’) but I couldn’t get that working.

    That issue is easy, it’s because this is determined by how a function is called, not where it’s defined. Since jQuery is calling your success function, this is set by jQuery.

    You can tell jQuery what this should be inside that callback via the context argument:

    $('#complete_task_1').click(function() {
      $.ajax({
        url: $(this).attr('href'),
        type: 'get',
        dataType: 'html',
        context: this,
        success: function(data, textStatus, jqXHR) {
            $(this).closest('tr').fadeOut();
        },
        error: function(data, status, error){
            alert('Oooops!');
          }
      });
    });
    

    …or since you’re already calling $(this) once and I hate to see unnecessary duplication, you can use the scope you’re already closing over:

    $('#complete_task_1').click(function() {
      var $this = $(this);
      $.ajax({
        url: $this.attr('href'),
        type: 'get',
        dataType: 'html',
        success: function(data, textStatus, jqXHR) {
            $this.closest('tr').fadeOut();
        },
        error: function(data, status, error){
            alert('Oooops!');
          }
      });
    });
    

    My problem is that I need to reload the page manually after checking one box – otherwise, when I click another items checkbox, the js doesn’t seem to be called. It’s neither a success or error. There’s nothing in the dev log either.

    You’re using an ID in your code, so you’re only ever hooking up one checkbox (since IDs must be unique on the page). My guess is that having updated the status, when you refresh the page that one checkbox now represents a different item.

    Instead of using an ID selector, use a class selector or a structural selector matching all of your relevant checkboxes. It looks like you’re using an href on the checkbox to differentiate them, so none of the code changes except the markup, and the initial selector.

    So if you wanted to use a class on the checkboxes:

    $('.someclass').click(function() { ...
    

    Or if they’re all in a container, you could use a structural (in this case, descendant) selector:

    $('#the_container input:checkbox').click(function() { ...
    

    Update: Here’s a complete working example (live copy):

    HTML:

    <table id="container">
      <tbody>
        <tr>
          <td>
            <label>
              <input type="checkbox" data-href="http://jsbin.com/amiruf/1">
              One
            </label>
          </td>
        </tr>
        <tr>
          <td>
            <label>
              <input type="checkbox" data-href="http://jsbin.com/amiruf/2">
              Two
            </label>
          </td>
        </tr>
        <tr>
          <td>
            <label>
              <input type="checkbox" data-href="http://jsbin.com/amiruf/3">
              Three
            </label>
          </td>
        </tr>
      </tbody>
    </table>
    

    Those URLs just reply with

    Response 1

    …or 2 or 3.

    JavaScript:

    jQuery(function($) {
    
      $("#container input:checkbox").click(function() {
        var $this = $(this);
        $.ajax({
          url: $this.attr("data-href"),
          dataType: "text",
          success: function(data) {
            $this.closest("tr").fadeOut();
            display(data);
          },
          error: function() {
            display("Error");
          }
        });
      });
    
      function display(msg) {
        $("<p>").html(msg).appendTo(document.body);
      }
    
    });
    

    Off-topic: It looks like you’re using an href attribute on your checkboxes. That’s invalid HTML, href is not a valid attribute for input[type=checkbox]. You might consider changing that to data-href, which would make it valid as of HTML5.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
i got an object with contents of html markup in it, for example: string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I need to clean up various Word 'smart' characters in user input, including but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm looking for suggestions for debugging... If you view this site in Firefox or

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.