Using jQuery, dynamically switch the content of the first div to the secondary div within an li when

I am currently facing a challenge with my "In-Place Editing" feature implementation. I need to dynamically retrieve the ID of two divs within one li element. The first div is visible by default, while the second div is initially set to display: none. When the pen icon (span) is clicked, I aim to toggle between the visibility of these divs - setting the first div to display: none and the second div to display: block. Below is the jQuery code I have so far:

$('.edit-tools').each(function () {
             var pen = this;
             $(pen).click(function () {
                 var div_id_first = $(this).closest(".school").attr('id');
                 var div_id_second = $(".school-edit").attr('id');

                 console.log(div_id_first);
                 console.log(div_id_second);

                 $('#' + div_id_first).css("display", "none");
                 $('#' + div_id_second).css("display", "block");

                 $('.edit-tools').attr("disabled", true); 
             });
         });

UPDATE: The issue lies in retrieving the ID of the second div due to its placement outside the current scope. As the pen (span) is located within the first div, closest() method resolves the first div's ID successfully.

The primary concern remains on how to access the ID of the second div,

<li>
    <div id="09990-view"></div>
    <div id="09990-edit" style="display: none;"></div>
<li>

To view the jsfiddle example, click here: http://jsfiddle.net/tF6nD/3/

Answer №1

Here is a live demonstration: http://jsfiddle.net/kR7sX/9/

In order to access the child elements based on the li element, follow this code snippet:

         $('.modify-tools').click(function () {
             var first_div_id = $(this).closest("li").find(".subject").attr('id');
             var second_div_id = $(this).closest("li").find(".subject-edit").attr('id');

             console.log(first_div_id);
             console.log(second_div_id);

             $('#' + first_div_id).css("display", "none");
             $('#' + second_div_id).css("display", "block");

             $('.modify-tools').attr("disabled", true); 
         });

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

Issue with OnChange event in HTML and Adding Content with Jquery

I'm currently working on an app that includes a dynamic dropdown feature using jQuery and the append() method to display changing numbers dynamically. Everything seems to be functioning well in the first field of the form, but when I try to add more f ...

Why is the Jquery selected change not functioning on IE9?

Hey there, I've got this click function that's working smoothly on Chrome and Firefox but doesn't seem to do anything on IE9. Whenever I click the dropdown box, the click function doesn't execute on IE9. Here's the script: $(&ap ...

Alignment of images in a grid layout using Bootstrap 4

I am currently working on constructing an image grid with Bootstrap 4. The grid is contained within the class "showcase." In the first row, there are two landscape photos, and in the second row, there are three portrait photos. My challenge is to align the ...

How to toggle the dropdownbox status using jQuery

Just starting with jQuery and I'm looking to enable or disable a dropdown list based on a checkbox. Here's what my html looks like: <select id="dropdown" style="width:200px"> <option value="feedback" name="aft_qst">After Quest&l ...

Tips for organizing data and dynamically linking options to another select value?

One of my challenges involves working with two select elements. The first select allows for multiple options, while the second select is dependent on the choice made in the first one. <select class="form-control" id="select1"> <option value=""& ...

Using Jquery to display text when the condition is not met

Here is the code snippet I am currently working with: http://jsfiddle.net/spadez/mn77f/6/ I am trying to make it display a message when there are no fields (questions) present. I attempted to achieve this by adding the following lines of code: } else ...

Trail of crumbs leading to pages displayed in a div container

My website is designed with only one page where all other pages are loaded within a div. I am looking to implement breadcrumbs to keep track of the pages loaded inside the div. However, the solutions I have found online only work for pages loaded in the en ...

Transformations in Object Attributes Following an AJAX Submission

For my latest project, I am developing an application using node.js and express. One of the functionalities involves making a POST request that sends an array of strings to the server. However, upon inspecting the request body on the server side, I noticed ...

form for submitting multiple data via Ajax

I am working with two forms (request and feedback) where I need to use jQuery Ajax to send the data. When a user submits a request, the subject line will display "Request". If they submit feedback, the subject line will display "Feedback". Here is my cur ...

Flexible columns with dynamic maximum width based on percentage

After extensive research, I am turning to stackoverflow for help with a seemingly simple task that has proven difficult to achieve. I have three columns of expandable content and need them to adjust in size proportionally when one or more are expanded. Eac ...

Covering the heading of a collection of Bootstrap 4 card components

I currently have a bootstrap card group set up with a title that is contained within each individual card. However, I would like the title to span across the entire card group. Is there a way to achieve this? <div class="card-group"> ...

CSS - When using both display: flex and position: absolute, the anchor point is shifted

I have a flexbox element with three children. I want the second child to overlap the first child using absolute positioning, like this: Desired Outcome: https://i.stack.imgur.com/i479w.png It's important that the second child appears on top of the ...

How can I ensure that the AppBar background color matches the color of the navigation bar?

Having trouble changing the background color of the <AppBar> in my project. Using the Container component to set a maximum screen size, but encountering issues when the screen exceeds this limit. The AppBar background color appears as expected while ...

Data index is not defined

I am encountering an issue with Yajra\DataTables when trying to retrieve data from the controller using the following code: $services = DB::table('services')->where('status', '=', 3)->get(); return datatables($serv ...

Disable the moving of the DIV button with a left-margin of 0px

Having a bit of a challenge here. I'm attempting to craft my own slider using jQuery along with some css and javascript. I've managed to get my slider functioning, moving a Div 660px to the left and right upon button clicks. However, I'm h ...

Checkbox's checked attribute fails to toggle

Encountered a peculiar issue while trying to implement jQuery functionality triggered by a checkbox. The checkbox seems to be toggling the check mark visually on click, but the "checked" attribute remains unchanged. For a demo of the problem, check out: h ...

The value entered in the HTML input is not being recognized by PHP

I'm facing an issue with my project (index.php), which is a HTML webpage. I am trying to use the 'include' function to bring in the header and footer (header.php, footer.php). Additionally, I want the current date to automatically populate i ...

Guide to designing a single-row table with varying numbers of columns and ensuring alignment is in place

I currently have a table displayed https://i.sstatic.net/20gZm.png The issue arises when I input large amounts of data, causing the structure to become irregular as depicted above. My query revolves around maintaining proper alignment within the table eve ...

Mastering the use of divs in CSS-3

Is there a way to position div elements on a webpage in various locations? Which CSS property is most effective for managing this issue? I'm having trouble setting the positions of multiple divs at different points on a page. ...

jQuery scripts fail to load upon returning using the back button

Attempting to create a website similar to GitHub using PJAX. The page functions normally when utilizing PJAX links, but encounters issues when the back button is clicked. While the content loads successfully, the jQuery scripts are not ready. To see this ...