Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

innerHTML to plain text?

  • 23-10-2007 11:11PM
    #1
    Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭


    Trying to do a greasemonkey script and JS is not my area. :/

    I have an innerHTML string that contains HTML tags. I want to remove them all so it is only the text.

    Is there a way to do this?


Comments

  • Registered Users, Registered Users 2 Posts: 378 ✭✭sicruise


    function removeHTMLTags(){
    	if(document.getElementById && document.getElementById("input-code")){
    		var strInputCode = document.getElementById("input-code").innerHTML;
    		/* 
    			This line is optional, it replaces escaped brackets with real ones, 
    			i.e. &lt; is replaced with < and &gt; is replaced with >
    		*/	
    		strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
    			return (p1 == "lt")? "<" : ">";
    		});
    		var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
    		alert("Input code:\n" + strInputCode + "\n\nOutput text:\n" + strTagStrippedText);	
    	}	
    }
    

    I found this here... http://robertnyman.com/roblab/javascript-remove-tags.htm


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    excellent! Cheers.


Advertisement