I want only td (Test 1) change class(yellow).
How to make only td (Test 1) change color (not entire row).
$('input[name="cek"]').on('change', function() {
$(this).closest('tr').toggleClass('yellow', $(this).is(':checked'));
});
$(document).ready(function() {
$('input:checked[name="cek"]').closest('tr').addClass('yellow');
});
.yellow {
background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
<thead>
<tr>
<th nowrap>TGL</th>
<th>CEK</th>
<th>TITEL</th>
<th>VAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>01.12.19</td>
<td nowrap><input type='checkbox' name='cek' /></td>
<td nowrap>Test 1</td>
<td nowrap>1</td>
</tr>
</tbody>
</table>
You can try the below code.
$('input[name="cek"]').on('change', function() {$(this).closest('td').next('td').toggleClass('yellow',$(this).is(':checked'));});
.yellow{
background-color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" >
<thead>
<tr>
<th nowrap>TGL</th>
<th>CEK</th>
<th>TITEL</th>
<th>VAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>01.12.19</td>
<td nowrap><input type='checkbox' name='cek' /></td>
<td nowrap>Test 1</td>
<td nowrap>1</td>
</tr>
</tbody>
</table>
You can do something below
$('input[name="cek"]').on('change', function() {
// $(this).closest('td').toggleClass('yellow',$(this).is(':checked'));
$(this).parents('td').siblings("td:eq(1)").toggleClass('yellow',$(this).is(':checked'));
});
$(document).ready(function() {
$('input:checked[name="cek"]').closest('tr').addClass('yellow');
});
.yellow{
background-color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" >
<thead>
<tr>
<th nowrap>TGL</th>
<th>CEK</th>
<th>TITEL</th>
<th>VAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>01.12.19</td>
<td nowrap><input type='checkbox' name='cek' /></td>
<td nowrap>Test 1</td>
<td nowrap>1</td>
</tr>
</tbody>
</table>
Use parent() with next().
$('input[name="cek"]').on('change', function() {
$(this).parent().next('td').toggleClass('yellow', $(this).is(':checked'));
});
.yellow {
background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
<thead>
<tr>
<th nowrap>TGL</th>
<th>CEK</th>
<th>TITEL</th>
<th>VAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>01.12.19</td>
<td nowrap><input type='checkbox' name='cek' /></td>
<td nowrap>Test 1</td>
<td nowrap>1</td>
</tr>
</tbody>
</table>
Related
My table is here http://jsfiddle.net/wqk7Lauz/
All I need is to have all values from td:nth-of-type(3) multiplied with 3.8 on page load.
HTML
$('td:nth-of-type(3)').text(parseFloat($('td:nth-of-type(3)').text()) * 3.8)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
You're very close, you just need to use the callback version of text so you're only handling the text of each individual element, and remove the R$ at the beginning:
$('td:nth-of-type(3)').text(function() {
return parseFloat($(this).text().replace("R$", "")) * 3.8;
});
Example:
$('td:nth-of-type(3)').text(function() {
return parseFloat($(this).text().replace("R$", "")) * 3.8;
});
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
If you want to add back the R$, and perhaps limit the result to a specific number of places, you can do that with string concatenation and toFixed:
$('td:nth-of-type(3)').text(function() {
return "R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2);
});
Example:
$('td:nth-of-type(3)').text(function() {
return "R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2);
});
<table class="table table-bordered mydatatable table-hover display responsive nowrap">
<tbody>
<tr>
</tr>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço por 1000</th>
<th>Quantidade mínima</th>
<th>Quantidade máxima</th>
<th>Descrição</th>
</tr>
<tr>
<td>9</td>
<td>Product </td>
<td>
R$1.86
</td>
<td>100</td>
<td>10000</td>
<td>S1 </td>
</tr>
<tr>
<td>10</td>
<td>Product </td>
<td>
R$3.98
</td>
<td>1000</td>
<td>100000</td>
<td>S1</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Hi change your script function with this
$(".table tr td:nth-child(3)").each(function(){ //Loop
//alert($(this).text())
var thrdtd = $(this).text(); //Getting value of td
onlyno = thrdtd.replace('R$', ''); // Removing R$
//akert(thrdtd);
$(this).text('R$ '+ parseFloat(onlyno * 3.8).toFixed(2)) // Adding R$ and also multiplying at the same time and update that result to td again
});
In this first i am getting value of every third td value through loop
$(".table tr td:nth-child(3)").each(function(){
and then removing character from it before multiplying and then after multiplying i am updating the same td value
You can use the below code:
$('td:nth-of-type(3)').text("R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2));
I have a datatable as follows:
foreach($tickets as $tickets)
{
echo ('<tr>');
echo ('<td>'.$tickets->error.'</td>');
echo ('<td>'.$tickets->hours.'</td>');
echo ('<td>'.$tickets->time.'</td>');
echo ('<td>'.$tickets->date.'</td>');
echo ('</tr>');
}
I added accordion effect to it likewise:
foreach($tickets as $tickets)
{
echo ('<tr data-toggle="collapse" data-target=".demo1">');
echo ('<td>'.$tickets->error.'</td>');
echo ('<td>'.$tickets->hours.'</td>');
echo ('<td>'.$tickets->time.'</td>');
echo ('<td>'.$tickets->date.'</td>');
echo ('</tr>');
echo ('<tr>');
echo ('<td class="hiddenRow">');
echo ('<div class="collapse demo1">Demo1</div>');
echo ('</td>');
echo ('</tr>');
}
and the table has lost it's datatable properties such as search, view as 10/25/50 items per page etc.
Jquery:
$('.collapse').on('show.bs.collapse', function () {
$('.collapse.in').collapse('hide');
});
$(".clickable-row").click(function() {
window.location = $(this).data("href");
});
I'd like help regarding this issue.
Try using colspan="4" on the second td
Here is fiddle: http://jsfiddle.net/3ghbLrpu/1/
<html>
<head>
<script src="js/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<table id="dataTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Error</th>
<th>Time Spent</th>
<th>Time</th>
<th>Date</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Error</th>
<th>Time Spent</th>
<th>Time</th>
<th>Date</th>
</tr>
</tfoot>
<tbody>
<tr data-toggle="collapse" data-target=".demo1" class="accordion-toggle">
<td>asd</td>
<td>asd</td>
<td>asd</td>
<td>asd</td>
</tr>
<tr>
<td class="hiddenRow" colspan="4">
<div class="collapse demo1">Demo1</div>
</td>
</tr>
</tbody>
</table>
</body>
CSS
.hiddenRow {
padding: 0 !important;
}
Another option is use bootstrap and remove knockoutjs
https://codepen.io/creativedev/pen/XYMRyQ
$(document).ready(function() {
$("#collapseme").click(function() {
if($("#test").hasClass("out")) {
$("#test").addClass("in");
$("#test").removeClass("out");
} else {
$("#test").addClass("out");
$("#test").removeClass("in");
}
});
});
HTML
<table id="dataTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Error</th>
<th>Time Spent</th>
<th>Time</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr class="" id="collapseme">
<td>asd</td>
<td>asd</td>
<td>asd</td>
<td>asd</td>
</tr>
<tr>
<td class="hiddenRow" colspan="4">
<div class="collapse out" id="test">Demo1</div>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Error</th>
<th>Time Spent</th>
<th>Time</th>
<th>Date</th>
</tr>
</tfoot>
</table>
My html table goes like below, In the footer of my table i want to add the "Total" and sum of all the values at the footer of particular "quarters"
"quarter1" "quarter2" "quarter3" "quarter4"
250000 115000
175000 600000 275000
300000 150000 750000
650000 450000
850000 290000 145000
my view code is like below
How to overcome my issue, please suggest me. Any helps are appreciated,
Thank you
<table class="table table-bordered" id="referListTable">
<thead>
<tr>
<th>SI.No</th>
<th>Jan-Mar</th>
<th>Apr-Jun</th>
<th>Jul-Sep</th>
<th>Oct-Dec</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach($dashboardDetails as $llist){
?>
<tr>
<td><?php echo $i; ?></td>
<td align="right">$llist['previous']; ?></td>
<td align="right">$llist['quarter1'];?></td>
<td align="right">$llist['quarter2'];?></td>
<td align="right">$llist['quarter3'];?></td>
<td align="right">$llist['quarter4'];?></td>
</tr>
<?php
$i++;
}
?>
</tbody>
<tfoot>
<tr>
<th>Total</th>
<td id="total" ></td>
<td id="total" ></td>
<td id="total" ></td>
<td id="total" ></td>
<td id="total" ></td>
</tr>
</tfoot>
</table>
Try something like this
var sums = []
$("table tbody tr").each(function() {
$(this).find("td").each(function(i, x) {
if ($(x).html().length) {
var tdValue = parseInt($(x).html());
tdValue = (tdValue + parseInt(sums[i] == undefined ? 0 : sums[i]))
sums[i] = tdValue
}
})
})
$.each(sums,function(i,x) {
$("tfoot tr").append("<td>"+x+"</td>")
})
Note this is created before we had your html code in the question, but it should be able to give you an idea how to make it work with your own html code
var sums = []
$("table tbody tr").each(function() {
$(this).find("td").each(function(i, x) {
if ($(x).html().length) {
var tdValue = parseInt($(x).html());
tdValue = (tdValue + parseInt(sums[i] == undefined ? 0 : sums[i]))
sums[i] = tdValue
}
})
})
$.each(sums,function(i,x) {
$("tfoot tr").append("<td>"+x+"</td>")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<td>quarter1</td>
<td>quarter2</td>
<td>quarter3</td>
<td>quarter4</td>
</tr>
</thead>
<tbody>
<tr>
<td>250000</td>
<td>115000</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>175000</td>
<td>600000</td>
<td>275000</td>
</tr>
<tr>
<td>300000</td>
<td>150000</td>
<td>750000</td>
<td></td>
</tr>
<tr>
<td>650000</td>
<td></td>
<td></td>
<td>450000</td>
</tr>
<tr>
<td>850000</td>
<td>290000</td>
<td>145000</td>
<td></td>
</tr>
</tbody>
<tfoot>
<tr>
</tr>
</tfoot>
</table>
How to get data and set in to php var when use file_get_contents PHP ?
this is code for http://www.example.com/test.php
<table align="center">
<thead style="background-color: #F0F0F0; color: #333333; text-align: center;">
<tr>
<th class="countryHeader">Country</th>
<th class="intHeader"> </th>
<th colspan="2" class="intHeader">Desktop CPM</th>
<th colspan="2" class="bannerHeader">Mobile CPM</th>
</tr>
</thead>
<tbody>
<tr class="breaker">
<td> </td>
<td>Highest</td>
<td>Unique</td>
<td>Raw</td>
<td>Unique</td>
<td>Raw</td>
</tr>
<tr class="odd rateVals">
<td class="countryName">United States</td>
<td class="cpmRaw">$2.00</td>
<td class="cpm">$3.44</td>
<td class="cpmRaw">$2.00</td>
<td class="cpmMobile">$1.63</td>
<td class="cpmMobileRaw">$0.97</td>
</tr>
<tr class="even rateVals">
<td class="countryName">Canada</td>
<td class="cpmRaw">$1.20</td>
<td class="cpm">$2.52</td>
<td class="cpmRaw">$1.20</td>
<td class="cpmMobile">$1.88</td>
<td class="cpmMobileRaw">$1.06</td>
</tr>
<tr class="odd rateVals">
<td class="countryName">United Kingdom</td>
<td class="cpmRaw">$1.60</td>
<td class="cpm">$2.10</td>
<td class="cpmRaw">$1.08</td>
<td class="cpmMobile">$1.71</td>
<td class="cpmMobileRaw">$0.94</td>
</tr>
<tr class="even rateVals">
<td class="countryName">Australia</td>
<td class="cpmRaw">$1.50</td>
<td class="cpm">$2.62</td>
<td class="cpmRaw">$1.20</td>
<td class="cpmMobile">$2.73</td>
<td class="cpmMobileRaw">$1.49</td>
</tr>
<tr class="breaker">
<td colspan="6"> </td>
</tr>
<tr class="odd rateVals">
<td class="countryName">United Arab Emirates</td>
<td class="cpmRaw">$1.48</td>
<td class="cpm">$2.49</td>
<td class="cpmRaw">$1.45</td>
<td class="cpmMobile">$2.11</td>
<td class="cpmMobileRaw">$1.42</td>
</tr>
<tr class="odd rateVals">
<td>All other countries</td>
<td>-</td>
<td colspan="2">$0.25 - $2.00</td>
<td colspan="2">$0.06 - $0.22</td>
</tr>
</tbody>
</table>
And this is code for my_code.php
<?php
$test = file_get_contents('http://www.example.com/test.php');
//echo $test;
?>
And i want to get php var from column Mobile CPM raw value like this
$United_States = "0.97";
$Canada = "1.07";
$United_Kingdom = "0.94";
$Australia = "1.49";
$United_Arab_Emirates = "1.42";
How can i do ?
If you want to parse a HTML document you should use PHP's DOMDocument. Instead of file_get_contents() you should use loadHTMLFile(). Because you are using CSS-classes you should view this post on how to find elements via class.
I want to update tutor table and tutor course table, i have multiple checkbox for tutor course and if a teacher update his course or other info both table should also update.... tutor can increase or decrease course. I'm using following query but its not working for me
$update_content = mysql_query("UPDATE wp_tutor
JOIN wp_tutor_courses
ON wp_tutor.tutor_id=wp_tutor_courses.tutor_id
SET wp_tutor.tutor_name='$tNameVar',
wp_tutor.tutor_qualification='$tQualVar',
wp_tutor.tutor_skype='$tSkyVar',
wp_tutor.tutor_specialization='$tuSAreaVar',
wp_tutor.tutor_gender='$tuGenderVar',
wp_tutor_courses.course_id='$tuCourseVar'
WHERE tutor_id='$tid'");
<table cellpadding="5" cellspacing="0" border="1">
<caption>Tutor Table</caption>
<thead>
<tr>
<th>Tutor ID</th>
<th>Tutor Name</th>
<th>Tutor Skype ID</th>
<th>Tutor Specialization</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">Kashif</td>
<td align="center">kashiflatif</td>
<td align="center">Financial Accounting</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">Ammar</td>
<td align="center">ammar.90</td>
<td align="center">Research Methods</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">Bilal</td>
<td align="center">bilalhaider95</td>
<td align="center">Islamic Bond & Practices</td>
</tr>
</tbody>
</table>
<br />
<br />
<table cellpadding="5" cellspacing="0" border="1" style="float:left">
<caption>Tutor Course Table</caption>
<thead>
<tr>
<th>Tutor Course ID</th>
<th>Tutor ID</th>
<th>Course ID</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">1</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">1</td>
<td align="center">3</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">2</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">2</td>
<td align="center">3</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">3</td>
<td align="center">1</td>
</tr>
</tbody>
</table>
<table cellpadding="5" cellspacing="0" border="1" style="float:left; margin-left:20px;">
<caption>Course Table</caption>
<thead>
<tr>
<th>Course ID</th>
<th>Course Name</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">Certificate</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">Diploma</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">PGD</td>
</tr>
</tbody>
</table>
Try this:
$update_content = mysql_query("UPDATE wp_tutor a
INNER JOIN wp_tutor_courses b
ON a.tutor_id=b.tutor_id
SET a.tutor_name='$tNameVar',
a.tutor_qualification='$tQualVar',
a.tutor_skype='$tSkyVar',
a.tutor_specialization='$tuSAreaVar',
a.tutor_gender='$tuGenderVar',
b.course_id='$tuCourseVar'
WHERE a.tutor_id='$tid'");