/    Sign up×
Community /Pin to ProfileBookmark

can i get the contents of a PRE tag?

Hi Guys

I have some HTML :

[code=html]<pre>
Some stuff some more stuff some more stuff
</pre>[/code]

What I need to do is to access the contents of this <pre> and delete any tab spacing from the END of the line.

Any pointers on how I may be able to access the contents with javascript? document.getElementsByTagName(“pre”).value doesnt seem to be doing the trick. I cannot give the pre an id, as the html is auto generated and is out of my control.

Thanks in advance! ?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@CharlesSep 01.2006 — [code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script type="text/javascript">
onload = function () {document.getElementsByTagName ('PRE')[0].firstChild.data = document.getElementsByTagName ('PRE')[0].firstChild.data.replace (/t+$/, '')}
</script>

</head>
<body>
<pre> Some stuff some more stuff some more stuff </pre>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@bsojitraauthorSep 01.2006 — charles you are a legend mate! thanks so much.

out of interest, how would i access the other children of the pre? i.e. if i want ed to access the second or third child?

thanks!

[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script type="text/javascript">
onload = function () {document.getElementsByTagName ('PRE')[0].firstChild.data = document.getElementsByTagName ('PRE')[0].firstChild.data.replace (/t+$/, '')}
</script>

</head>
<body>
<pre> Some stuff some more stuff some more stuff </pre>
</body>
</html>[/code]
[/QUOTE]
Copy linkTweet thisAlerts:
@bsojitraauthorSep 01.2006 — no worries.. i have it.

document.getElementsByTagName("pre")[0].childNodes[1];
Copy linkTweet thisAlerts:
@CharlesSep 01.2006 — The PRE element can only have one child.
Copy linkTweet thisAlerts:
@bsojitraauthorSep 01.2006 — i just realised that i made a mistake in my original post. my pre is:

<pre>

<img src="myimage.gif">some stuff some more stuff some more stuff


</pre>

so the firstchild here is an image object. i can access the text by saying document.getElementsByTagName('PRE').childNodes[1].data.

seem to work.. but is this the correct way of doing it?
Copy linkTweet thisAlerts:
@CharlesSep 01.2006 — See http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html for all the different tools available for your use.
Copy linkTweet thisAlerts:
@bsojitraauthorSep 01.2006 — Thanks Charles.

For testing purposes, when I alert :

[CODE]alert(document.getElementsByTagName('PRE').childNodes[1].data.replace(/s+$/, '***'));[/CODE]

It seems to replace the spaces at the end with the stars as desired in the alert popup. But when I actually try to use the code :

[CODE]document.getElementsByTagName('PRE').childNodes[1].data.replace(/s+$/, '***');[/CODE]

..it doesnt do anything! Any ideas why this may be?? (i have replace /t with /s to remove ALL spaces).

Thanks in advance.
Copy linkTweet thisAlerts:
@bsojitraauthorSep 01.2006 — nevermind i got it to work.
×

Success!

Help @bsojitra spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...