Identify the descendant of the `<li>` tag

I need help creating a interactive checklist. The idea is that when you click on an item in the list, a hidden child element should become visible.

<div id="list_one">
    <h2>LIST TITLE</h2>
    <div id="line"></div>
    <ul>
        <li id="1-1"><div class="blue_line"></div><div class="circle"></div>TASK ONE</li>
        <li id="1-2"><div class="blue_line"></div><div class="circle"></div>TASK TWO</li>
        <li id="1-3"><div class="blue_line"></div><div class="circle"></div>TASK THREE</li>
        <li id="1-4"><div class="blue_line"></div><div class="circle"></div>TASK FOUR</li>
        <li id="1-5"><div class="blue_line"></div><div class="circle"></div>TASK FIVE</li>
        <li id="1-6"><div class="blue_line"></div><div class="circle"></div>TASK SIX</li>
    </ul>
</div>

Below is my current Javascript implementation:

$(document).ready(function() {
    $(".blue_line").css("visibility","hidden");

    $("li").click(function() {
        var id = this.id;
        console.log(id);
        $(id).children().css("visibility","visible");
    });
});

Answer №1

Instead of using the id in that way, you can simply utilize $(this) to access the LI element:

$(document).ready(function() {
    $(".blue_line").css("visibility","hidden");
    $("li").click(function() {
        $(this).children().css("visibility","visible");
    });
});

It was noted before that the issue arose from using $(id) when you should have used $('#' + id) to correctly select the ID with jQuery

Answer №2

Remember to always add the # symbol:

$(document).ready(function() {
    $(".blue_line").css("visibility","hidden");
    $("li").click(function() {
        var id = this.id;
        console.log(id);
        $("#" + id).children().css("visibility","visible");
    });
});

It is more advisable to utilize $(this) in the event handler:

$(document).ready(function() {
    $(".blue_line").css("visibility","hidden");
    $("li").click(function() {
        $(this).children().css("visibility","visible");
    });
});

Answer №3

Not quite sure if I understand correctly, but here's a possible solution:

$("li").on('click', function() {
    $(this).toggleClass('chosen');
});

Link to the fiddle example: http://jsfiddle.net/Q8Gbp/1/

"When an item is clicked, apply a chosen class which adds a blue line."

This approach is more elegant - using a class makes it less disruptive and allows for greater flexibility in styling the li element when it has the .chosen class (it's generally recommended not to directly add styles to elements with JavaScript).

PS: Try to avoid using IDs excessively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the audio to not function properly on the webpage I'm working on that has yet to be launched?

As I work on my webpage, I am attempting to incorporate an audio file from SoundCloud. However, it seems that the file is not playing when sourced directly from the SoundCloud website. It works fine when the source is a file stored on my computer. <aud ...

Should I assign a variable to $(this).data("whatever") or not?

Does jQuery data() involve any CPU performance in retrieving values? Here's the scenario: I have multiple links/buttons like the one below <a href='#' class='ui-btn' data-counter='1234'>test</a> When a link ...

After installing Ember and running the Ember server, I noticed that the node_modules directory appears to be empty. This issue is occurring on my Windows 10 x64 PC

Welcome to the command console: C:\Users\Documents\emberjs>ember server node_modules seem to be empty, consider running `npm install` Ember-cli version: 2.14.2 Node version: 6.11.2 Operating System: Windows 32-bit x64 I'm a beg ...

When CSS is applied, the canvas is stretched, whereas it remains normal when `width` and `height` attributes are used

I own two canvases, one is sized using HTML attributes width and height, while the other is sized using CSS: <canvas id="canvas1" width="300" height="300" onmousedown="canvasClick(this.id);"></canvas> < ...

"Maximizing Search Efficiency: PHP Ajax Live Search Supporting Multiple Values

Looking to enhance my Php Ajax Live search functionality by adding a dropdown element for a more refined search. How can I integrate the dropdown value into the existing script? <input type="text" name="value1" id="value1"> <select id="value2" n ...

Angular Code Splitting with Webpack

My current project setup is causing some loading issues due to the large download size of Angular Material. As a result, a white screen remains loading for around 45 seconds. I have attempted to implement code splitting to enhance the loading speed of my a ...

What would be the best method for refreshing CSS following the dynamic addition of data using jQuery for optimal efficiency?

This paragraph was inserted following an ajax request: <tr id="product1" class = "success"> <td>1</td> <td>product one</td> </tr> The success class gives the row a green background, but this style is not applie ...

Having trouble defining the width of cards in Bootstrap 4 using precise percentage values

Take a look at this example. I believe I should be able to manually set the width of card elements using CSS. Despite my efforts, the width seems to be ignored and the final computed value is not what I expected. For instance, on larger screens, all 6 ca ...

Guide on organizing users into groups and filtering them using Firestore's reference field type

I currently manage a small group of employees with plans to expand in the future. To streamline operations, I am looking to implement a grouping feature that allows users to sort employees based on their assigned groups. Although I thought about using the ...

Align a bootstrap navigation item to the right without a dropdown menu

I'm having trouble positioning the Login/Register link to the right of my navbar. I've checked other solutions on this platform, but they either involve dropdown menus or don't work for me. Here's my Navbar code: <link href="http ...

Node.js API requests often result in undefined responses

As a newcomer to Node.JS, I am currently experimenting with calling a REST API using the GET method. I have utilized the 'request' package available at this link. While the call functions correctly, I encounter an issue when attempting to return ...

The ajax Success function fails to work when set to receive JSON data

My task involves adding an attendance record for a group of students within a classroom using a Bootstrap modal. Below is the code snippet for my Bootstrap modal, containing a form: <div id = "add_attendance_modal" class = "modal fade&qu ...

Email notification will be sent upon form submission to Firestore

I'm currently designing a website for booking reservations through an HTML form, with data submission to firestore. Upon submission, a confirmation email is sent to the customer. Below is the code snippet I am using to achieve this: var firestore = fi ...

Utilizing Angular's directive-based *ngIf syntax instead of passing a string parameter

Currently, I'm studying the article on reactive forms by PluralSight to brush up on my knowledge and I'm having trouble understanding the syntax provided below. <div *ngIf courseForm.get('courseName').valid && courseForm.get ...

Align the Media center div within a column utilizing Bootstrap

I have a template in html with a media object containing a logo and text, both within a column of width 5. Currently, it is not centered and aligned to the left. I am looking for a way to centrally align the media block within the column, regardless of its ...

Stop the removal of the CSS content property

When a user enters and re-enters their password, I have a form that checks the strength of the password and displays text accordingly. However, I noticed that if a user makes a mistake and uses the backspace to re-enter the password, the text from data-tex ...

Running a node.js project on the client side without using npm for deployment

Looking for a solution to efficiently deploy my nodejs project with frequent updates. The site does not have npm available, so I have to package the node_modules every time, which results in a large file size (80MB) that takes a long time to send via ftp t ...

Having trouble deploying a Heroku app using Hyper? Here's a step-by-step guide to

After running the following commands: https://i.stack.imgur.com/WZN35.png I encountered the following errors: error: src refspec main does not match any error: failed to push some refs to 'https://git.heroku.com/young-brook-98064.git' Can anyon ...

Create partial form copies dynamically based on user selections within a nested form

Attempting to design a form that dynamically generates copies of a nested form based on user input. Three models are involved: Visualizations, Rows, and Panes. The goal is to enable users to select from a dropdown menu, create a row, and choose 1, 2, or 3 ...

JS focusing on incorrect entity

Check out the fiddle link below to see the issue: https://jsfiddle.net/0a0yo575/10/ The goal is to display a single box in the top-left corner based on user interaction. However, the current JavaScript is causing the point to disappear instead of the box ...