kobra
kobra

Reputation: 1315

Check if text in cell is bold

I have a table in html. Some cells is bold and some is normal. How I can check if some cell is bold (for example i,j) and get text of that cell without tag .

Cells can have only text. If it bold - it should contain tag <b>. For example:

<tr>
<td>
Not bold text
</td>
<td>
<b> Bold text </b>
</td>
</tr>

PS I can't use class or id properties.
PSS It would be better without jQuery code, we are not using it for now

Upvotes: 1

Views: 3924

Answers (3)

Aaron Digulla
Aaron Digulla

Reputation: 328760

See this answer how to get a reference to the cell: https://stackoverflow.com/a/3052862/34088

You can get the first child with cell.firstChild. This gives you a text node or a <B> node. You can check this with the node.nodeType which is 1 for DOM nodes or 3 for text nodes. Which gives:

function isBold(table, rowIdx, columnIdx) {
    var row = table.rows[rowIdx];
    var cell = row.cells[columnIdx];
    var node = cell.firstChild;
    if( node.nodeType === 3 ) {
        return false;
    }

    return node.nodeName === 'b' || node.nodeName === 'B';
}

Upvotes: 2

mixable
mixable

Reputation: 1158

Use the css font-weight property to detect if the text is in bold:

if ($("#td-id").css("font-weight") == "bold") {
  var boldText = $("#td-id").html();
  // Text is bold
}

If you don't use css, there must be a <strong>-tag (or <b>-tag) inside the table cell contents. You may extract the bold text with the following code:

var boldText = $("#td-id strong").html();
if (boldText != NULL) {
   // Text is bold
}

Upvotes: 1

user557419
user557419

Reputation:

It completely depends on your HTML. If you use <b> or <strong> tags, then you can easily get text from those cells: $('TD strong').text();

If you make the text bold via CSS class, then you'd require the css class: $('TD.classForBoldText').text();

But if you've just made the text bold via generalizational css such as TD { font-weight: bold; } then you can't get the text of specific cells.

Upvotes: 0

Related Questions