/    Sign up×
Community /Pin to ProfileBookmark

Using JS to change an HTML code

Hello, I’m a bit new to JavaScript.

I was wondering, is it possible to change the text in the actual source of a website using JS?

Example:
A page starts out with a blue 5 on it.
Code starts out as:

[CODE]<font color=blue>5</font>[/CODE]

A button is pushed to change the text…
Code changes to:

[CODE]<font color=blue>20</font>[/CODE]

When the page is refreshed, the blue 20 is still there.

If this is possible, can someone please explain how to do it?
Thanks.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KramyMar 31.2006 — Oh sorry, misread you. You can't affect the source of a page, but a page can be built to adjust based on variables. PHP, for example, can have things put into the address, which affect the page. This edit page has:
[CODE]http://www.webdeveloper.com/forum/editpost.php?do=editpost&p=542802[/CODE]
That tells it to change the layout to include an advanced edit box, and also the post number I'm replying to or editting.

[b]--------Original Message--------[/b]

You can assign an ID to certain elements in the body.

[CODE]<table><tr><td id="changeme">hi<td></tr></table>
<div id="changeme2">bOO</div>[/CODE]
then use javascript:
[CODE]<script type="text/javascript">
<!--
var NewHTML = '<img src="blank.gif">';
document.getElementById('changeme').innerHTML = NewHTML
//-->
</script>[/CODE]

You can combine this with onClick and other functions to get effects like those orange/blue arrows on the right side of this page.

The below code will work if pasted into a .html file:
[CODE]<html>
<head>

<script type="text/javascript">
<!--
var SomethingIsTrue = true;

function ChangeContent()
{
if(SomethingIsTrue)
{
var NewHTML = '<img src="http://www.webdeveloper.com/forum/images/buttons/collapse_tcat_collapsed.gif" onClick="ChangeContent();"> <--- w00t, now it is blue!';
SomethingIsTrue = false;
}
else
{
var NewHTML = '<img src="http://www.webdeveloper.com/forum/images/buttons/collapse_tcat.gif" onClick="ChangeContent();"> <--- w00t, now it is orange!';
SomethingIsTrue = true;
}

document.getElementById('ReplaceMe').innerHTML = NewHTML
}
//-->
</script>

</head>
<body>

<table border="0" width="100%"><tr><td id="ReplaceMe">


<img src="http://www.webdeveloper.com/forum/images/buttons/collapse_tcat.gif" onClick="ChangeContent();"> <--- Click That!


</td></tr></table>

</body>
</html>[/CODE]


Google [URL=http://www.google.ca/search?hl=en&q=innerHTML+tutorial&btnG=Google+Search&meta=]innerHTML tutorial[/URL].
Copy linkTweet thisAlerts:
@hyperliskMar 31.2006 — You could semi- do it with JavaScript, using id's and innerHTML, in combination with GET variables and window.onload
×

Success!

Help @DarkGoten 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,
)...