New to this
New to this

Reputation: 21

Editing table values on load

How to delete a character of our choice...?

Say in a table we have values which have "www.example.com" Suppose I want to remove "www" from all the entries in a table... how to do that?

Suppose table id is "tab09".

How can I write a code in js such that as the page loads the chars "www" are removed from tab09?

Upvotes: 2

Views: 373

Answers (2)

Alexander Wallin
Alexander Wallin

Reputation: 1394

Using regular Javascript you would do:

var modifyTable = function(){
    var table = document.getElementById("tab09"),
        tds = table ? table.getElementsByTagName("td") : null;

    if (tds) {
        for (var i = 0; i < tds.length; i++) {
            tds[i].innerHTML = tds[i].innerHTML.replace(/www\./, "");
        }
    }
};

if (document.addEventListener) // DOM ready
    document.addEventListener("DOMContentLoaded", modifyTable, false);
else if (window.attachEvent) // IE, just use onload this time.
    window.attachEvent("onload", modifyTable);

EDIT: As thirtydot pointed out, without listening to DOM readiness, the code must be placed after the table element. Added listener.

Upvotes: 1

thirtydot
thirtydot

Reputation: 228182

Something like this should work:

$(document).ready(function() {
 $('#tab09 td').each(function() {
  $(this).html($(this).html().replace(/^www\./,''));
 });
});

Upvotes: 1

Related Questions