Using HTML, CSS, and jQuery to create a master-detail feature

I'm looking to implement a master-detail layout, so I decided to follow this tutorial for guidance:

While trying to replicate the tutorial, I encountered an issue with my code.

Here's the code snippet that I am working on:

HTML/jQuery

<!DOCTYPE html>
<html>
<head>
    <link href="stile.css" rel="stylesheet" type="text/css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("td:nth-child(1)").html("<img src='images.jpg' class='clk'>");

            $(".clk").click(function () {
            var index = $(this).parent().parent().index();
            var detail = $(this).parent().parent().next();
            var status = $(detail).css("display");
            if (status === "none")
                $(detail).css("display", "table-row");
            else
                $(detail).css("display", "none");
            });
        });            
    </script>
</head>
<body>
    <table id="tbSales" rules="rows"> 
        <thead> 
            <tr> 
                <th></th> 
                <th>ID</th> 
                <th>Date</th> 
                <th>Items</th> 
                <th>Total</th> 
            </tr> 
        </thead> 
        <tbody> 
            <tr class="saleRow"> 
                <td></td> 
                <td>01</td> 
                <td>01/01/2001</td> 
                <td>2</td> 
                <td>10,00</td> 
            </tr> 
            <tr class="itemsRow"> 
                <td colspan="5"> Itens 
                    <table class="tbItems" rules="rows"> 
                        <thead> 
                            <tr> 
                                <th>ID</th> 
                                <th>Description</th> 
                                <th>Quantity</th> 
                                <th>Unit Price</th> 
                            </tr> 
                        </thead> 
                        <tbody> 
                            <tr> 
                                <td>A1</td>
                                <td>Product A 1</td> 
                                <td>1</td> <td>7,00</td> 
                            </tr> 
                            <tr> 
                                <td>A2</td> 
                                <td>Product A 2</td>
                                <td>1</td> 
                                <td>3,00</td>
                            </tr> 
                        </tbody> 
                    </table> 
                </td> 
            </tr> 
            <tr class="saleRow"> 
                <td></td>
                <td>02</td> 
                <td>02/02/2001</td>
                <td>3</td> 
                <td>20,00</td>
            </tr> 
            <tr class="itemsRow">
                <td colspan="5"> Itens 
                    <table class="tbItems" rules="rows"> 
                        <thead> 
                            <tr> 
                                <th>ID</th> 
                                <th>Description</th> 
                                <th>Quantity</th> 
                                <th>Unit Price</th> 
                            </tr> 
                        </thead> 
                        <tbody> 
                            <tr> 
                                <td>B1</td> 
                                <td>Product B 1</td> 
                                <td>1</td> 
                                <td>10,00</td> 
                            </tr> 
                            <tr> 
                                <td>B2</td> 
                                <td>Product B 2</td> 
                                <td>2</td> 
                                <td>5,00</td> 
                            </tr> 
                        </tbody>
                    </table> 
                </td> 
            </tr> 
        <tbody> 
    </table>
</body>

CSS:

#tbSales, .tbItems{ 
width:100%; 
border:solid 1px; 
}

#tbSales thead th, .tbItems thead th{
text-align:left; 
} 

#tbSales tr td, .tbItems tr td{ 
border:solid 1px; 
} 

#tbSales tr td:nth-child(1){
width:20px; 
}

#tbSales tbody tr{
background:#DCDCDC; 
} 

.tbItems tr{
background:red; 
} 

#tbSales thead{
background:#4682B4; 
}

.itemsRow{ 
display: none; 
} 

.tbItems{ 
font-size:12px; 
} 

This is what the expected behavior should be:

However, when implementing it, the row appears empty. Why could this be happening?

Answer №1

$("td:nth-child(1)")

Modifying all initial table elements.

To target a specific cell, you must indicate the row:

$(".saleRow td:nth-child(1)")

Answer №2

The initial line of javascript you wrote will replace the contents of all first children in your table.

Instead of using:

$("td:nth-child(1)").html("<img src='images.jpg' class='clk'>");

Try using:

$("td:nth-child(1)").css("border","red dashed 2px")

and you'll see the difference.

Check it out here: http://codepen.io/jeremythille/pen/qELyWX

td:nth-child(1) refers to "Every first <td> of each <tr> within the entire table". Keep in mind, this may change if there are nested tables.

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

Creating a production-ready webpack bundle with minimized dependencies

Suppose there exists a module on npm known as "awesomepackage". I am able to declare it as a dependency for my application using package.json by executing: npm i --save awesomepackage Upon examining my node_modules directory, I come across a folder label ...

Difficulty Uploading Files

I'm facing an issue when trying to upload multiple files. When I select more than 1 djz_file, no information is obtained from $_POST and $_FILES. However, if it's a single file, everything works as expected. <fieldset> ...

Updating the total of textboxes using jQuery

I'm working on a jQuery function that totals the values of 7 textboxes and displays the result in a grand total textbox: $(document).ready(function() { $('.class2').keyup(function() { var sum = 0; $('.class2').each(funct ...

Display and conceal elements within predetermined time intervals using jQuery, while ensuring that the final element remains visible

Is there a way to automatically hide div1 and show div2 after a set amount of time, let's say 10 seconds or 15 seconds? I came across this thread: Show and hide divs at a specific time interval using jQuery, However, the solution provided in the po ...

Eliminate redundant tags using jQuery

I have a code snippet that I need help with. I want to check for duplicates and if found, display an alert stating that it already exists so users can't insert the same word/tag again. Can someone please assist me? <div id="tags"> <span>a ...

Can a JavaScript function be sent back via AJAX from PHP?

Can a javascript function be returned via Ajax from php? Typically, I would just return a value and handle it in plain javascript. However, since I am working on an Apache Cordova mobile app, I need to approach things differently. account = localStorage.g ...

Generate SVG components without displaying them

Is there a way to generate a custom SVG graphic through a function without the need to attach it to any element? Can I simply create an empty selection and return that instead? Here is my current implementation: function makeGraphic(svgParent) { retur ...

Triggering createEffect in SolidJS with an external dependency: A guide

Is there a way to use an external dependency to trigger the createEffect function in Solid, similar to React's useEffect dependency array? I am trying to execute setShowMenu when there is a change in location.pathname. const location = useLocation() ...

Is a Page Refresh Required to Reload Routes in ReactJS?

I have created menu links labeled Home, About, What I Do, and Experience for my portfolio site using BrowserRouter. However, when I click on these links, the components do not load properly on the page; they only render correctly after a page refresh. This ...

Utilizing CSS to apply unique styles to individual radio buttons

I have 4 radio buttons in my quiz application. I need to style only one of them, which is the correct answer option. Here's the HTML snippet that was generated: <p> <input id="SelectedAnswer" type="radio" value="5" name="SelectedAnswer"> ...

Problem with JQuery: Click to reveal the code

Here is some of my JQuery code jQuery('.tpcoupon-reveal').click( function() { jQuery('.inner-code').css('background-color','#FFF'); jQuery('.inner-code').text(jQuery(this).attr('caption')); visit ...

"ASP.NET MVC issue: Ajax request is returning a view instead of the expected ajax

I have a situation where an ASP.NET MVC call to a method via AJAX is throwing an exception error. I want the exception message to be sent back to the client without having to catch the exception explicitly. Here's what I'm trying to achieve: [Ht ...

Positioning a fixed div can result in the page being cut off on certain mobile browsers that are not

While there are numerous similar issues out there, the bug I am encountering feels unique. This issue involves a page with a div in position:fixed style, specifically affecting certain less common mobile browsers like Star Safari, DU Browser, and Tint Brow ...

Deactivating all HTML DOM elements except for those with a specific #id

Currently, my goal is to deactivate all elements on an html page while keeping certain elements active. $('body').find('*').attr('onclick',"return false;"); //Deactivating all elements // I am attempting to activate the eleme ...

javascript/jquery form validation problems/support needed (jQuery)

Long story short, I am facing an issue with my code and seeking some guidance. I have various functions to perform different checks. For this particular example, I have a form with a default value of "enter name here" for one field. Here is the HTML snipp ...

Implementing a sticky header for tables using Bootstrap

I'm experiencing a conflict because the header remains fixed only when I include the table-responsive class in my code. However, I also need the table to have overflow. Can anyone provide assistance with this issue? Using the table-responsive class ...

Setting a constant width for text characters across all devices using CSS and HTML

I am looking to display text in my textarea with the same width in pixels across all devices. Essentially, I want to set the character width in pixels so that it appears consistently on any device (Desktop/Mobile). <html> <head> <styl ...

What could be causing the bottom of a vertical scroll bar to be cropped in Internet Explorer 11?

I am experiencing an unusual problem in IE11 where the content at the bottom of my page is being cut off along with the scroll bar that should allow for viewing that content. I have managed to resolve this issue in Chrome, Firefox, and Edge but I cannot se ...

Add a third-party library file to Visual Studio

I'm currently working in Visual Studios and attempting to utilize the library provided at . However, I am encountering difficulties when trying to import the library. I have added the file to the project and attempted to use it within the Book.js (Vi ...

What exactly is HTML cloud storage all about?

As I work on developing an app through phonegap, one question that comes to mind is the possibility of storing information online. For instance, if there's a number variable that increases when a button is pressed, can this value be saved somewhere an ...