Reputation: 181
I have table in this format:
<tr>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
</tr>
<tr>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
</tr>
when click on some SPAN i would like that all the spans inside the specific TR will get grey color.
how can I do that?
element.parent("tr").children("span").attr('style', 'color: #838282 !important');
Upvotes: 0
Views: 43
Reputation: 30557
You can try this JavaScript code:
$('tr span').click(function(){
$(this).closest('tr').find('span').css('color', '#838282');
});
Upvotes: 0
Reputation: 115212
span
is not direct child so you need to use find()
and tr
is parent of td
not parent of span
so use closest()
. For applying style use css()
, also there is no need of !important
in css()
.
$('tr span').click(function(){
$(this).closest("tr").find("span").css('color', '#838282');
});
Upvotes: 2
Reputation: 1809
$("span").on("click",function()
{
$(this).parents("tr").find("span").css('color', '#838282');
});
Upvotes: 0
Reputation: 1252
parents(selector)
get the match selector in ancestorsfind(selector)
get the match children in children treeRun the code snippet to see result
$(document).ready(function() {
$("span").click(function() {
$(this).parents("tr").find("span").attr('style', 'color: #838282');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<table>
<tr>
<td><span>A</span>
</td>
<td><span>B</span>
</td>
<td><span>C</span>
</td>
</tr>
<tr>
<td><span>D</span>
</td>
<td><span>E</span>
</td>
<td><span>F</span>
</td>
</tr>
</table>
Upvotes: 1