html - Automatically get info for ::before element with JavaScript -
i've been trying figure out wait work having multiple tables on single web page.
<table class="table table-striped basic"> <thead> <tr> <th>saturday</th> <th class="time">10:30am</th> <th class="time">11:00am</th> <th class="time">11:30am</th> <th class="time">12:00pm</th> </tr> </thead> <tbody> <tr> <td>3/5/2016</td> <td>15231</td> <td>15232</td> <td>15233</td> <td>15234</td> </tr> <tr> <td>3/5/2016</td> <td>15231</td> <td>15232</td> <td>15233</td> <td>15234</td> </tr>
<script> var time = $('.time').text(); $(function() { $('table td:nth-child(2)').attr('data-before-content', time); $('table td:nth-child(3)').attr('data-before-content', time); $('table td:nth-child(4)').attr('data-before-content', time); $('table td:nth-child(5)').attr('data-before-content', time); }); </script>
css:
.table td:nth-child(2)::before { content: attr(data-before-content); }
trying figure out way context of each "th" , place ::before in css while having multiple tables. suggestions?
are thinking along lines of this? can use combination of .each()
, .eq()
, , .not()
$('table tr').each(function(){ $('td', this).not(':first-child').each(function(i){ $(this).attr('data-before-content', $('.time').eq(i).text()); }); });
.table td:before { content: attr(data-before-content) '\00a0'; } .table td { padding: 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-striped basic"> <thead> <tr> <th>saturday</th> <th class="time">10:30am</th> <th class="time">11:00am</th> <th class="time">11:30am</th> <th class="time">12:00pm</th> </tr> </thead> <tbody> <tr> <td>3/5/2016</td> <td>15231</td> <td>15232</td> <td>15233</td> <td>15234</td> </tr> <tr> <td>3/5/2016</td> <td>15231</td> <td>15232</td> <td>15233</td> <td>15234</td> </tr> </tbody> </table>
in case .time
data table specific (which more case think), i'd recommend using instead:
$('table tr').each(function(){ var $t = $(this).closest('table').find('.time'); $('td', this).not(':first-child').each(function(i){ $(this).attr('data-before-content', $t.eq(i).text()); }); });
.table td:before { content: attr(data-before-content) '\00a0'; } .table td { padding: 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-striped basic"> <thead> <tr> <th>saturday</th> <th class="time">10:30am</th> <th class="time">11:00am</th> <th class="time">11:30am</th> <th class="time">12:00pm</th> </tr> </thead> <tbody> <tr> <td>3/5/2016</td> <td>15231</td> <td>15232</td> <td>15233</td> <td>15234</td> </tr> <tr> <td>3/5/2016</td> <td>15231</td> <td>15232</td> <td>15233</td> <td>15234</td> </tr> </tbody> </table> <table class="table table-striped basic"> <thead> <tr> <th>monday</th> <th class="time">7:30pm</th> <th class="time">9:00pm</th> <th class="time">10:30pm</th> </tr> </thead> <tbody> <tr> <td>3/7/2016</td> <td>15231</td> <td>15232</td> <td>15233</td> </tr> <tr> <td>3/7/2016</td> <td>15231</td> <td>15232</td> <td>15233</td> </tr> </tbody> </table>
Comments
Post a Comment