Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help removing html from variable

Status
Not open for further replies.

TurboSRT4

Programmer
Nov 23, 2009
47
US
hello all! I am looking for help... I need to remove html code from variables. For example I have this.

document.change.n_first.value=document.getElementById('fname').innerHTML;

ass of right now
document.getElementById('fname').innerHTML;

is "<a href=link>please help me</a>

how can i remove the html tags from
document.getElementById('fname').innerHTML;

so that
document.getElementById('fname').innerHTML;
is simply "please help"

thank u guys
 
I would have thought that the answer was obvious, given you clearly know how to use "innerHTML" - instead of getting the innerHTML of the outer element, simply get the innerHTML of the anchor instead!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
<script>
var x = document.getElementById("fname").innerHTML;
var y = x.substr(x.indexOf(">") + 1, x.length - (x.indexOf(">") + 1));
y = y.substr(0, y.indexOf("<"));
document.getElementById("fname").innerHTML = y;
</script>
 
Hi

I would say, using a nutcracker-sledgehammer to crack a nut. Because that sledgehammer will not work for a hazelnut like "<a href=link>please <b>help</b> me</a>".

Once I would just get the [tt]a[/tt] tag's [tt]innerHTML[/tt] as Dan suggested, but my experience turned me towards the regular expressions :
JavaScript:
document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'fname'[/i][/green][teal]).[/teal]innerHTML[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'fname'[/i][/green][teal]).[/teal]innerHTML[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/<[^<>]+>/g[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal])[/teal]

[gray]// or shorter and faster[/gray]

[b]with[/b] [teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'fname'[/i][/green][teal]))[/teal] innerHTML[teal]=[/teal]innerHTML[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/<[^<>]+>/g[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal])[/teal]
Or you can use the [tt]textContent[/tt] property in Gecko ( FireFox, SeaMonkey, Galeon ), Presto ( Opera ), WebKit ( Safari, Chrome, Epiphany, Midori, Arora ), KHTML ( Konqueror ) :
Code:
[b]with[/b] [teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'fname'[/i][/green][teal]))[/teal] innerHTML[teal]=[/teal]textContent
Or the [tt]innerText[/tt] property in Presto ( Opera ), WebKit ( Safari, Chrome, Epiphany, Midori, Arora ), KHTML ( Konqueror ), Trident ( Explorer ) :
Code:
[b]with[/b] [teal]([/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'fname'[/i][/green][teal]))[/teal] innerHTML[teal]=[/teal]innerText

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top