We help IT Professionals succeed at work.

Javascript remove <a> tag Firefox issue

lcor
lcor asked
on
This is a followup to a question I asked.  Here's the question:
I have a javascript function for a printer friendly page.  The only problem is that hyperlinks show up in the printer friendly page.  I need the text within the <a> tags without the links.

How do I remove just the <a> tags from document object?  Please, no CSS -- I need to get the javascript below working.

Here's the javascript code:

var html = document.getElementById("spacer").innerHTML;
var winprint = window.open("", "", sOption);

winprint.document.write(html);


The answer was

winprint.document.write(html.replace(/<a[^>]*>(.*?)<\/a>/gi,'$1'));

I tried it on Internet Explorer and it works but does not work on Firefox 10.

Any ideas why and how to fix?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
You might try:

winprint.document.write(html.replace(/<\/?a[^>]*>/gi,''));

Open in new window

Commented:
Do you need to remove only the tags or the whole node including text between the tags?
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
CSS is probably the best option for you, so not sure why you wouldn't want to use that!

Here's an easy jQuery way to do it.

$('a').each(function(){
	$(this).replaceWith($(this).html());
});

Open in new window

Author

Commented:
To answer question, just want the <a tag removed, not the text.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.