/    Sign up×
Community /Pin to ProfileBookmark

document.getElementById not updating

I’m a new user and new to JS, so please bear with me.

I have 3 files in the same folder, main.html, test.html and script.js

In my main.html, I have

`script src=”script.js”, and <p id = “test”></p>`
In my test.html, I have

`script src=”script.js”, and <p id = “tt”></p>`
In my script.js, I have a function:

“`
function test() {
document.getElementbyId(“test”).innerHTML = “this is in main.html”
document.getElementbyId(“tt”).innerHTML = “this is in test.html”
{
“`

But, when I call the function with a button, only the main.html

with id “test” is updated with “this is in main.html”, how come the

with id “tt” in test.html is not updated with “this is in test.html”?

I can provide files up on request

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinAug 07.2020 — document.getElement b yId("tt").innerHTML = "this is in test.html"

typo I guess ?


this will fix it ...
``<i>
</i>function test() {
var el;
if(el=document.getElementById("test"))el.innerHTML = "this is in main.html";
if(el=document.getElementById("tt"))el.innerHTML = "this is in test.html";
}<i>
</i>
``
Copy linkTweet thisAlerts:
@tonynsxauthorAug 10.2020 — Sorry, no its doing the same thing. Its only updating the "test" tag, not "tt".

is var el supposed to be var eel?

function test() {<br/>
var el;<br/>
el = document.getElementById("tt")<br/>
if(el=document.getElementById("test"))<br/>
el.innerHTML = "this is in main.html";<br/>
if(el=document.getElementById("tt"))<br/>
el.innerHTML = "this is in test.html";<br/>
}


if I make var el document.getElementById("tt") or document.getElementById("test"), I get the same result. It prints "this is in main.html", same thing if I make document.getElementById("asdlfjadf")

No matter what I make el, it does the same thing
Copy linkTweet thisAlerts:
@tonynsxauthorAug 10.2020 — also tried this with sajme results, alerts this is in main.html. I'm not sure why it can't 'read' tt

let eel = document.getElementById("tt")<br/>
alert(eel)<br/>
if(eel=document.getElementById("main"))<br/>
alert("this is in main.html")<br/>
if(eel=document.getElementById("tt"))<br/>
alert("this is in test.html")
Copy linkTweet thisAlerts:
@rpg2009Aug 10.2020 — > @tonynsx#1621976 is var el supposed to be var eel?

It's short for 'element'. It's a variable name, so excluding reserved words can be anything you want. For instance var el, var elem, var element etc
Copy linkTweet thisAlerts:
@rpg2009Aug 10.2020 — In a simple test, with all three files in the same folder, based on DaveyErwin's script it works.

**page-01.html**
``<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang='en'&gt;
&lt;head&gt;
&lt;meta charset='UTF-8'&gt;
&lt;meta name='viewport' content='width=device-width, initial-scale=1.0'&gt;
&lt;title&gt;Page 1&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1 id='page-01'&gt;&lt;/h1&gt;
&lt;button id='test'&gt;Test&lt;/button&gt;
&lt;script src='script.js'&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;<i>
</i>
`</CODE>
<STRONG>**page-02.html**</STRONG>
<CODE>
`<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang='en'&gt;
&lt;head&gt;
&lt;meta charset='UTF-8'&gt;
&lt;meta name='viewport' content='width=device-width, initial-scale=1.0'&gt;
&lt;title&gt;Page 2&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1 id='page-02'&gt;&lt;/h1&gt;
&lt;button id='test'&gt;Test&lt;/button&gt;
&lt;script src='script.js'&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;<i>
</i>
`</CODE>
<STRONG>**script.js**</STRONG>
<CODE>
`<i>
</i>document.addEventListener('DOMContentLoaded', function (event) {
const buttonHandler = function (event) {
const elem = document.querySelector('#page-01, #page-02')

if (elem.id === 'page-01') elem.textContent = 'this is in page-01.html'
if (elem.id === 'page-02') elem.textContent = 'this is in page-02.html'
}

const button = document.querySelector('#test')
button.addEventListener('click', buttonHandler)
})<i>
</i>
``

Outputs 'this is in page-01.html' for page-01 and 'this is in page-02.html' for page-02
×

Success!

Help @tonynsx 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 4.24,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...