1

I'm almost there with something thanks to the help of stackoverflow users. I just need a bit more help with this one.

I have a HTML page with a dynamically generated table of data which I cannot alter view. It's returned like this:

<tr>
<td class="name">foo<</td>
<td class="time">5:36:13</td>
<td class="avtime">0:09:36</td>
<td class="transcripts">0</td><td class="views">35</td>
<td class="percent">100</td>
</tr>

What I need to do is find and replace:

<td class="percent">$foo</td>

With:

<td class="percent"><span class="bar" style="width:$foo%"></span></td>

As I say I was almost there and have replacements working just not with variable values.

What I have now:

function replaceScript() {
var toReplace = '<td class="percent">69</td>';
var replaceWith ='<td class="percent"><span class="bar" style="width:69%"></span>  </td>';
document.body.innerHTML = document.body.innerHTML.replace(toReplace, replaceWith);
}

Can you help?

Thanks in advance

3
  • Is this an actual element, or are you manipulating a string before adding it to the DOM? Commented Apr 1, 2012 at 19:12
  • @DavidThomas - as you can see from the reference to document.body.innerHTML in their code it's the actual page DOM. Commented Apr 1, 2012 at 19:23
  • ...I feel such a fool for even asking... XD Commented Apr 1, 2012 at 19:26

4 Answers 4

2

Rather than messing with the entire page HTML directly (which whacks all event handlers and forces a browser reparse of the entire page and can be fooled by different browser's re-manufacture of the HTML), I would suggest this DOM manipulation:

$("td.percent").each(function() {
    if (this.innerHTML == "69") {
        this.innerHTML = '<span class="bar" style="width:69%;"></span>';
    }
});

If you want the style width to be taken from the HTML directly and use any value you find there, then you can do it like this:

$("td.percent").each(function() {
    var widthVal = this.innerHTML;
    this.innerHTML = '<span class="bar" style="width:' + widthVal + '%;"></span>';
});
Sign up to request clarification or add additional context in comments.

3 Comments

I think your on to something however I need to get the width from the content of the td.percent. Can this be done?
@user1306706 - Based on your comment - I added a second option that gets the width percentage from the previously existing HTML.
@user1306706 - there was a typo (an extra paren) in the second example that I fixed.
1

Is $foo fixed? Why don't you use regular expressions?

var str = document.body.innerHTML;
var pattern = /($foo)/gim;
str.replace(pattern, "<span class=\"bar\" style=\"width:$1%\"></span>");

Comments

1

Use a RegExp to search and replace as follows:

function replaceScript () {
  var toReplace = /<td class="percent">(\d+)<\/td>/g;
  var replaceWith ='<td class="percent"><span class="bar" style="width:$1%"></span>  </td>';
  document.body.innerHTML = document.body.innerHTML.replace (toReplace, replaceWith);
}

Comments

1

Using plain javascript

function replaceScript()
{
    var tbl=document.getElementsByTagName('table');
    for(var t=0;t<tbl.length;t++)
    {
        var cols=tbl[t].getElementsByTagName('TD');

        for(var i=0;i<cols.length;i++)
        {
            if(cols[i].className=="percent")
            {
                var span=document.createElement('span');
                span.className="bar";
                span.innerHTML=cols[i].innerHTML; // if you want to populate the span with td's data/innerHTML, otherwise remove this line
                span.style.width=cols[i].innerHTML+'%';
                cols[i].innerHTML='';
                cols[i].appendChild(span);   
            }
        }
    }
}

Here is an example.

Edit Or may be this one

function replaceScript()
{
    var tbl=document.getElementsByTagName('table');
    for(var t=0;t<tbl.length;t++)
    {
        var cols=tbl[t].getElementsByTagName('TD');
        for(var i=0;i<cols.length;i++)
        {
            if(cols[i].className=="percent")
            {
                var spanData=cols[i].innerHTML;
                cols[i].innerHTML='<span class="bar" style="'+cols[i].innerHTML+'%">'+spanData+'</span>';

            }
        }
    }
}

Here is an example.

6 Comments

Might have to be careful if there might ever be other class names on the same object with "percent". Any reason you didn't use getElementsByClassName()?
I think it's not supported by all browsers (if I'm not wrong).
I either use a selector library like Sizzle or the selector in jQuery or use a shim for getElementsByClassName(). Browser compatibility for it is here: caniuse.com/getelementsbyclassname. All save lots of coding and mistakes.
It's true, anyway, don't you consider that IE8 is still being used and it has no support for this method so may be we should not rely on className method only for this (IE8).
I said I would use either a library like Sizzle or jQuery that solves the compatibility problem or I'd use a shim for getElementsByClassName() that solves it. My answer to this question uses jQuery because the OP tagged the question with jQuery.
|

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.