I'm displaying some data in a table and for each row I have another one with some details that is hidden by default and I want to make it visible only when the user clicks a link.
HTML looks like this:
<div id="listing">
<?php for(): ?>
<tr>
<td>Toggle</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr class="details" style="display: none">
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<?php endfor; ?>
</div>
I tried using jQuery like so, but with no results:
$('#listing').on('click', '.toggle', function(e) {
e.preventDefault();
$(this).closest('tr .details').toggle();
});
On each first row element provide it a data-target this will be which class to target and toggle the show/hide.
So basically on click for toggle class we get the data-target use that value to find our target and then simply toggle the target.
JsFiddle : https://jsfiddle.net/63ujphmj/
Javascript
$(function()
{
$('.toggle').on('click', function()
{
var target = $(this).data('target');
$('.'+target).toggle();
});
});
Html
<div id="listing">
<table>
<tr>
<td>Toggle 1</td>
<td>Toggle 2</td>
<td>Toggle 3</td>
<td>Toggle 4</td>
</tr>
<tr class="details">
<td class="details-1" style="display: none">Toggle 1 details</td>
<td class="details-2" style="display: none">Toggle 2 details</td>
<td class="details-3" style="display: none">Toggle 3 details</td>
<td class="details-4" style="display: none">Toggle 4 details</td>
</tr>
</table>
</div>
Assuming you fix the invalid HTML and include <table> tags, you want to use:
$('#listing').on('click', '.toggle', function(e) {
e.preventDefault();
$(this).closest('tr').next('.details').toggle();
});
You need to use .closest('tr') to traverse up in the DOM from the button clicked to the parent row, then .next('.details') to move to the next row element.
Related
I am trying to attach an on-click event to a table cell so when its clicked its background changes colour and text saying 'Yes' appears in the middle. I would also like it to fire a PHP method in a controller class.
I have code such as the following:
// index.html
```
<table>
<thead>
<tr>
<th>Heading</th>
</tr>
<thead>
<tbody>
<tr class="clickme">
<td><a onclick="reserve();return false;" href={{route('submitaction',
$colour->colour_id) }}</a></td> // On click, change its background
colour.
<td></td>
</tr>
</tbody>
// scripts.js
function reserve(){
$(this).addClass('newColour');
}
//flow.live.css
.newColour {
background-color: gray;
content: 'Yes';
text-align: center;
}
I am going to pad the link to fill the entire table cell and have the event fire when the user clicks the link. Is what I am trying to do sensible and are there any other clever work-arounds?
Thanks,
Rob
I have used a different markup, because you did not provide the CSS.
What you need is to delegate the event to the parent element.
I have used this markup from another website:
<table id="customers">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Königlich Essen</td>
<td>Philip Cramer</td>
<td>Germany</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td>North/South</td>
<td>UK</td>
</tr>
<tr>
<td>Paris spécialités</td>
<td>Marie Bertrand</td>
<td>France</td>
</tr>
</table>
Here is the JS:
$( "#customers" ).on( "click", "td", function( event ) { // target the outermost parent element, this will delegate the event to the element specified as second arg on the callback function
$(this).addClass('newColour'); // Here you just add the class, no inline JS needed.
});
That is it, read about event delegation and why it is useful and try the working code here, there is no PHP code, but your biggest issue was the JS.:
https://codepen.io/damPop/pen/JwZXya
Based on the question asked here: https://www.quora.com/How-do-I-display-a-div-table-when-a-link-is-clicked
I used the code given by the top answer.
Now, my code is modified to use variables:
echo ''.$row['Name'] .'';
It's in a while loop.
<div id="data-table" style="visibility: hidden;">
<table>
<?php
if $Name ==
?>
</table>
</div>
My div is supposed to show the data for the item whose Name is selected. eg: if "Robin" is selected, Robin's data will be shown. so how do I get the div to check which Name/link was clicked and then show the data for that Name/link?
Your question is a little hard to follow. Hopefully this can clear up some of your front-end issues.
HTML:
<div class="container">
<h3>Triggers</h3>
<p class='table-reference' data-record-ref='1'>Highlight Row #1</p>
<p class='table-reference' data-record-ref='2'>Highlight Row #2</p>
<p class='table-reference' data-record-ref='3'>Highlight Row #3</p>
<hr>
<h3>Table</h3>
<table id="record-table">
<thead>
<tr>
<th>ID</th>
<th>Data</th>
</tr>
</thead>
<tbody>
<tr data-record='1'>
<td>#1</td>
<td>Record</td>
</tr>
<tr data-record='2'>
<td>#2</td>
<td>Record</td>
</tr>
<tr data-record='3'>
<td>#3</td>
<td>Record</td>
</tr>
</tbody>
</table>
</div>
Javascript/jQuery:
(function($) {
$(function() {
$("*[data-record-ref]").click(function() {
var id = $(this).data("record-ref");
var element = $("*[data-record='"+id+"']");
if(!!element) {
$(this).toggleClass("highlight");
element.toggleClass("highlight");
} else {
console.error("Record Not Find.");
}
});
});
}) ($);
Demo
I have a table with the image and image code data, show on code below:
<table id="tblForklift" cellpadding="5" cellspacing="0">
<tbody>
<tr id="image_info">
<td id="1W 1111">
<img src="../Client/images/forklift/corpus-christi-forklifts1.jpg" width="210px" height="210px"/>
<p style="text-align: center;">1W 1111</p>
</td>
<td id="2W 2222"></td>
<td id="3W 3333"></td>
</tr>
<tr id="image_info"></tr>
<tr id="image_info"></tr>
<tr id="image_info"></tr>
</tbody>
</table>
I tried using this code to get the html of selected td of the table. But it show "undefined".
$('#tblForklift').on('click', function(e) {
var forkliftCode = $('this').closest('tr').find('td').html();
alert(forkliftCode)
});
Since #tblForklift will match your table. You need to target td elements inside this table instead. Also if your elements has been added dynamically to the DOM, you can use event delegation:
$(document).on('click', '#tblForklift tr td', function(e) {
var forkliftCode = $(this).html();
alert(forkliftCode)
});
or better if your table is not added dynamically:
$('#tblForklift').on('click', 'tr td', function(e) {
var forkliftCode = $(this).html();
alert(forkliftCode)
});
Also some of your td are missing closing </td>
Add event to the td. so in each td click you can get html.
$('#tblForklift td').on('click',function(e) {
alert($( this ).html());
});
demo
id must be unique use class like,
HTML
<table id="tblForklift" cellpadding="5" cellspacing="0">
<tbody>
<tr class="image_info">
<td class="1W 1111 forklift">
<img src="../Client/images/forklift/corpus-christi-forklifts1.jpg" width="210px" height="210px"/>
<p style="text-align: center;">1W 1111</p>
</td>
<td class="2W 2222"></td>
<td class="3W 3333"></td>
</tr>
<tr class="image_info"></tr>
<tr class="image_info"></tr>
<tr class="image_info"></tr>
</tbody>
</table>
SCRIPT
$('.forklift').on('click', function(e) { // using class for multiple tds
var forkliftCode = $(this).find('p').text();
alert(forkliftCode);
});
Also, never give a space in a id, space has a different meaning in jquery selectors
Live Demo
HTML:
<td class="2W 2222">cvbcxbcvb</td>
<td class="3W 3333">bcvbcvbnvnv</td>
</tr>
<tr class="image_info"></tr>
<tr class="image_info"></tr>
<tr class="image_info"></tr>
</tbody>
</table>
jQuery:
$('#tblForklift td').click(function() {
alert($(this).html());
});
I have a table with each rows have a Send Button:
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Address</th>
<th>Button</th>
</tr>
</thead>
<tbody>
<tr>
<td id="name"></td>
<td>20</td>
<td>Street AA</td>
<td><a name="sendName" id="sendId" href="www.google.com">Send</a>
</td>
</tr>
<tr>
<td>Mr.XX</td>
<td>20</td>
<td>Street XX</td>
<td><a name="sendName" id="sendId" href="www.google.com">Send</a>
</td>
</tr>
<tr>
<td>Mr.YY</td>
<td>20</td>
<td>Street YY</td>
<td><a name="sendName" id="sendId" href="www.google.com">Send</a>
</td>
</tr>
</tbody>
</table>
Preview
All values in that table I get from database. So what I want to do is, when the NAME value is empty, the send button will be disabled based on its row table.
Since you have no input values i do not quite understand why you want to use javascript for a task like this. If the value is empty simply do not display the link at all. Or remove the href-attribute add a css class and style the link different.
//disabled link
<a name="sendName" id="sendId">Send</a>
I don't know if this was a copy/paste error but you use the same ID sendID on all links.
You really need to not put the button there if the database returns an empty name instead of removing it after. Something like
<a name="sendName" id="<? echo "id".&name; ?>"
<? if (&name!="") echo 'href="#"'; ?>>Send</a>
or
<? if (&name=="") { ?>
<span>Send</span>
<? } else { ?>
<a name="sendName" id="<? echo "id".&name"; ?>" href="#">Send</a>
<? } ?>
Try the Below example code on send link :
<?php if($row['NAME']==''){ echo 'Send' } else{?>
<a name="sendName" id="sendId" href="www.google.com">Send</a>
<?php }?>
you can try this jquery code for that you you need to include jquery library
<script>
$(document).ready(function(e){
var objTR = $("table tr");
for(var i = 0; i < objTR.length; i++)
{
var objCols = $(objTR[i]).children();
var name = objCols[0].innerHTML;
if(name == "")
$(objCols[3]).find("a").removeAttr("href");
}
});
</script>
i have made this code to remove table row :
jQuery('tr#rowAttend1').remove();
HTML :
Three rows with same id .
For example :
<tr id="rowAttend1" ><td>ssss</td></tr>
<tr id="rowAttend1" ><td>ddddd</td></tr>
<tr id="rowAttend1" ><td>ccccc</td></tr>
but
i want all three to be removed
it works fine in all browsers except ie7 ?
Can anybody help me?
<tr id="rowAttend1" ><td>ssss</td></tr>
<tr id="rowAttend2" ><td>ddddd</td></tr>
<tr id="rowAttend3" ><td>ccccc</td></tr>
$('#rowAttend1,#rowAttend2,#rowAttend3').remove();
Change the ids to unique ids and then you can remove the table row
Class example
<tr class="rowAttend1" ><td>ssss</td></tr>
<tr class="rowAttend1" ><td>ddddd</td></tr>
<tr class="rowAttend1" ><td>ccccc</td></tr>
$('.rowAttend1').remove();
This will remove all elements with the class rowAttend
Your ID is not unique please use different ID for like,
<tr id="rowAttend1" ><td>ssss</td></tr>
<tr id="rowAttend2" ><td>ddddd</td></tr>
<tr id="rowAttend3" ><td>ccccc</td></tr>
(OR)
Pass particular eventcatch(e) (i.e) (this) to that javascript function and remove from that this like,
$(this).remove();
some different way try if u like
<table>
<tr><td>one</td> <td><button class="removeTR"> one</button></td> </tr>
<tr><td>two</td> <td><button class="removeTR"> two</button></td> </tr>
<tr><td>three</td> <td><button class="removeTR">three</button></td> </tr>
<tr><td>four</td> <td><button class="removeTR"> four</button></td> </tr>
<tr><td>five</td> <td><button class="removeTR"> five</button></td> </tr>
</table>
$("tr .removeTR").live('click', function() {
$(this).parent().parent().remove();
});
or some thing that gives good smooth row deleting
$("tr .removeTR").live('click', function() {
$(this).parent().parent().fadeOut(1000);
setTimeout(function(){$(this).parent().parent().remove();}, 1000);
});