I have a table
element on some page. It contains some table rows with cells, containing href
s.
My target is: when I point mouse cursor over som
Best method would be to set class to any cell with the specific text while making the HTML. If it's impossible, You can use simple script to do so on load:
<!DOCTYPE html >
<html>
<head>
<title> Bla! </title>
<style type='text/css'>
td.boldOn:hover { font-weight:bold; color:red; }
</style>
<script type='text/javascript'>
function Init() {
var obj = document.getElementById('tblId');
for (var i =0, row; row=obj.rows[i]; i++) {
for (var j=0, cell; cell= row.cells[j]; j++) {
if( cell.innerHTML.indexOf('ing') != -1) {
cell.className = 'boldOn';
}
}
}
}
</script>
</head>
<body onload='Init();'>
<table id='tblId' border='1'>
<tr>
<td> not bold </td>
<td> bolding </td>
<td> not bold </td>
<td> bolding </td>
</tr>
<tr>
<td> not bold </td>
<td> bolding </td>
<td> not bold </td>
<td> bolding </td>
</tr>
</table>
</body>
</html>
If you can do it ahead - then simply to the 'bolding' td's add :
<td class='boldOn'> bolding </td>
and all the js is useless.
http://jsfiddle.net/f82KA/14/
<table>
<tr><td><a href="#">Android</a><a href="#">Applesometext</a><a href="#">Symbiansometext</a></td><td><a href="#">Android</a></td></tr>
<tr><td><a href="#">Windows</a><a href="#">Symbiansometext</a><a href="#">Applesometext</a></td><td><a href="#">Windows</a></td></tr>
</table>
var p = /sometext/;
var s = document.querySelectorAll('td a');
function boldAll(b) {
for(var i=0;i<s.length;i++) {
if(b) {
if(p.test(s[i].innerHTML)) {
s[i].style.fontWeight = 'bold';
}
} else {
s[i].style.fontWeight = 'normal';
}
}
}
function h(e) {
if(p.test(e.target.innerHTML)) {
boldAll(true);
}
}
for(var i=0;i<s.length;i++) {
s[i].addEventListener('mouseover',h);
s[i].addEventListener('mouseout',function(e) {
boldAll(false);
});
}
Using jQuery (which you said may be acceptable) it could be something like this:
$(document).ready(function(){
$('td > a').hover(
function() {
$('td > a:contains("' + this.innerHTML + '")').css('font-weight', 'bold')
},
function() {
$('td > a').css('font-weight', 'normal')
}
)
})
DEMO: http://jsfiddle.net/2ATc5/3/
When mouse enters anchor within TD - code looks for all anchors within TDs that have the same text and bolds them.
When mouse leaves the anchor - font-weight returns to normal.