0

I'm trying to make a javascript function that will find all the <td> tags in a string and make them red. That way I can track down an errant tag who lacks his comanion </td>. My problem is that when I copy the html of a page, it loses all of the indentation structure. Is there anyway to keep this structure?

$(document).ready(function(){
   var html = $('body').html(); 

   html.replace('<td>', '<td><span class="red">'); 
   html.replace('</td>', '</td></span>'); 
   $('#result').text(html);
});

http://jsfiddle.net/KL3u3/2/

​Also, the string replacements don't seem to work at all. But one thing at a time.

Thanks for any ideas!

12
  • 1
    Wouldn't it be the span inside the td and not backwards? Commented Nov 29, 2012 at 17:15
  • @Diego Yeah, definitely. Corrected. Thank you Commented Nov 29, 2012 at 17:17
  • @Diego But no actually.. because the idea is to print out the html structure, turning it into text (so you actually see <td><span>content</span></td> on the page) and then the characters <td> will be red. Commented Nov 29, 2012 at 17:28
  • 1
    You can't do that like that. You can't output a html string as text and expect some elements to be text and some elements to be actual HTML with working styles etc. It's one or the other, and the only solution to what you're proposing would be to output text and html in seperate functions. Commented Nov 29, 2012 at 17:30
  • 1
    You cannot get the raw source of your HTML document (see also this question). Whenever you stringify the parsed DOM, you will get a valid structure - the html parser already handled the errant </td>. Commented Nov 29, 2012 at 17:47

2 Answers 2

1
$.ajax({
    url: document.location,
    dataType: "html" // get plain source
}).done(function(text) {
    $(function() {
         $("body").text(text).html(function(_, old) {
             return old.replace(/&lt;\/?td&gt;/g, '<span class="red">$&</span>');
         }).css({"white-space":"pre-wrap", "text-align":"left", "font-family":"monospace"});
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for this, but it does not seem to work for me. www2.iscotest.com/test/getsource.html Have I implemented it incorrectly?
Works for me (in Opera). Do you get any errors, which browser are you using? Might be an implementation-dependent serialisation of &lt;td&gt;. For a safe way have a look at stackoverflow.com/a/10416898/1048572
Sorry, Bergi, I had to step out for a bit. This works perfectly actually, Thank you! Can you tell me, where to _ and old come from?
It's a replace function for api.jquery.com/html - _ is the unused index, old is the current innerHTML.
0

String replace only replaces the first occurence. To do a global replace you would need to use regex.

var html = $('body').html(); 
    html = html.replace(/(<td>)/gi, '<td><span class="red">'); 
    html = html.replace(/(<\/td>)/gi, '</span></td>'); 
$('#result').text(html);

FIDDLE

Also notice that string replace is immutable and will have to be declared back to the variable again.

Why not just wrap the TD's with jQuery ?

$('body').find('td').wrap('<span class="red" />');

1 Comment

This seems a good idea, but unfortunately the fiddle isn't working for me. :/

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.