0

How do I do a find/replace on specific elements in a datatable? I'm trying to replace elements in the table with font awesome icons:

JavaScript:

$(document).ready(function() {
  $('#example').dataTable();
  $('td').html().replace('0', '<i class="fa fa-thumbs-o-up"></i>');
  $('td').html().replace('1', '<i class="fa fa-warning"></i>');
} );

JFiddle: http://jsfiddle.net/wo02xedr/

3 Answers 3

2

Iterate the text and replace using the old parameter:

$('td').text(function (i, old) {
    $(this).html(old.replace('0', '<i class="fa fa-thumbs-o-up"></i>').replace('1', '<i class="fa fa-warning"></i>'))
});

jsFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Some issue with your jsfiddle!
1

Some bit of your code is wrong, Here you go..

$(document).ready(function() {
    $('#example').dataTable();  
    $("td").each(function() {
   $(this).html( $(this).html().replace('0', '<i class="fa fa-thumbs-o-up"></i>').replace('1', '<i class="fa fa-warning"></i>'));

});
});

Here is the jsFiddle Link http://jsfiddle.net/wo02xedr/2/

2 Comments

Thanks @FREAKENGINEER. How do I just do it on one table? When I put a table object in instead of "td" it doesn't like it: jsfiddle.net/wo02xedr/9
You can not iterate thorugh #example, Their is just one element having id "example"..
1

I see some improvements :

  1. first you should loop on each element by using $("td").each(function() {...})
  2. update the html of your tds (here you only read it) with : $this.html(...)

This gives something like this :

$('td').each(function () {
    var $this = $(this);

    $this.html($this.html()
        .replace('0', '<i class="fa fa-thumbs-o-up"></i>')
        .replace('1', '<i class="fa fa-warning"></i>'));
});

See this fiddle : http://jsfiddle.net/wo02xedr/8/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.