Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

innerHTML to plain text?

Options
  • 23-10-2007 11:11pm
    #1
    Registered Users 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 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 Posts: 21,264 ✭✭✭✭Hobbes


    excellent! Cheers.


Advertisement