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 7892197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:48:35+00:00 2026-06-03T06:48:35+00:00

I am having a difficulty when trying to make an if-statement with $(this).attr(id) .

  • 0

I am having a difficulty when trying to make an if-statement with $(this).attr("id"). Want I want is that when a checkbox is clicked AND checked, its ID is logged in a div #log (this works). When it is clicked and not checked, jQuery should check whether a logged line is the same as the ID of the checkbox. If so, that span should get removed.

Here is a jsfiddle: http://jsfiddle.net/fLskG/1/

So when you click Option 1 the text option-0 will appear (this is the checkbox’s ID). When you click the button again, that text should disappear.

Here is the script:

$(document).ready(function() {
    $(".menu").find("input:checkbox").click(function() {

        if ($(this).is(":checked")) {
            $("#textValue").text($(this).val());
            $("#log").append("<span>" + this.id + "</span><br/>");
        }
        else {
            if ($("#log").children("span").contains($(this).attr("id"))) {
                console.log($(this).attr("id"));
                $("#log").children("span").remove();
            }
        }
    });
});​

I am quite sure the problem lies with the ‘contains’. But I don’t know how to make jQuery check whether there is a span which contains the ID of the checkbox checked in the first function.

Thanks.

  • 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-03T06:48:37+00:00Added an answer on June 3, 2026 at 6:48 am

    You could adopt the HTML5 data attribute, but since it’s one-to-one, I just went with giving the span an id of log-{checkbox-id}, so basically:

    $(".menu input:checkbox").on("click", function() {
        if ($(this).is(":checked")) {
            $("#textValue").text($(this).val());
            $("#log").append("<span id=\"log-" + this.id + "\">" + this.id + "</span><br/>");
        }
        else {
            $("#log-" + $(this).attr("id")).remove();
        }
    });​
    

    Here’s the fiddle: http://jsfiddle.net/crazytonyi/fLskG/7/

    The nice thing with this is that you don’t have to confirm if the value already is in the list, since it just won’t be found with the selector. I would probably tighten up how you did the insert and HTML as well, but for now, I just wanted to modify the code enough to show the id tag solution.

    Here it is, a bit tightened up:

    $(".menu input:checkbox").on("click", function() {
        if ($(this).is(":checked")) {
            $("#textValue").text($(this).val());
            var $log_entry = $('<li>');
            $log_entry.attr("id", "log-" + this.id);
            $log_entry.text(this.id);
            $("#log").append($log_entry);
        }
        else {
            $("#log-" + $(this).attr("id")).remove();
        }
    });​
    

    Notice that the log is now a ul so no need for a break, etc, making it easier to add the id attribute and text. New demo at: http://jsfiddle.net/crazytonyi/fLskG/12/

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

Sidebar

Related Questions

I'm having difficulty with Javascript instance variables. I'm trying to make an easy way
I'm trying to use this library (which looks very nice) but I'm having difficulty
I am having difficulty trying to get modernizer to run with codeigniter. this is
I'm having difficulty with a dictionary that I want to compare an updated version
I am having difficulty making any sense of this! What is he trying to
Im having difficulty trying to design a database structure for the following scenario: My
I'm having difficulty trying to sum up my question in form of a google
im having some difficulty trying to pull out a specific value from a cookie.
Trying to convert the following but am having difficulty. Can anyone see where I'm
I'm having a great deal of difficulty trying to figure out the logic behind

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.