/    Sign up×
Community /Pin to ProfileBookmark

Javascript is going crazy!

Hello.

I am sharing with you the following code:

[CODE]
<html>
<body>

<button onclick=”myCoolFunction()”>Push The Button!</button>

<p id=”here1″></p>
<p id=”here2″></p>
<p id=”here3″></p>
<p id=”here4″></p>

<script>
function myCoolFunction() {
var str = ‘hey I am a text<div class=”r”><a href=”https://www.heyiamalink1.com” ping=hey I am a super text<div class=”r”><a href=”https://www.heyiamalink2.com” ping=’;

var n1 = str.search(‘class=”r”><a href=”‘);
var n2 = str.search(‘” ping=’);
var str1 = str.slice(n1+19,n2);

document.getElementById(“here1”).innerHTML = str.length;
document.getElementById(“here2”).innerHTML = n2;
document.getElementById(“here3”).innerHTML = str1;

var str2 = str.slice(n2,str.length);

document.getElementById(“here4”).innerHTML = str2;

}
</script>

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

The output should be:

[CODE]
154

67

https://www.heyiamalink1.com

” ping=hey I am a super text<div class=”r”><a href=”https://www.heyiamalink2.com” ping=
[/CODE]

But for some reason it is:

[CODE]
154

67

https://www.heyiamalink1.com

” ping=hey I am a super text
[/CODE]

instead. Does anybody understand it? Am I doing something wrong?

Thank you.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinAug 15.2020 — @codewitch#1622136 said...

Am I doing something wrong?

- - - - - - - - -


let's look at just part of your code ...
``<i>
</i>&lt;html&gt;
&lt;body&gt;
&lt;button onclick="myCoolFunction()"&gt;Push The Button!&lt;/button&gt;
&lt;p id="here1"&gt;&lt;/p&gt;
&lt;p id="here2"&gt;&lt;/p&gt;
&lt;p id="here3"&gt;&lt;/p&gt;
&lt;p id="here4"&gt;&lt;/p&gt;
&lt;script&gt;
function myCoolFunction() {
document.getElementById("here1").innerHTML = 'str.length';
document.getElementById("here2").innerHTML = 'n2';
document.getElementById("here3").innerHTML = 'str1';
document.getElementById("here4").innerHTML = 'str2';
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``

If you reply to this I will show you what you are doing wrong.
Copy linkTweet thisAlerts:
@rpg2009Aug 15.2020 — Try swapping the **.innerHTML**s for **.textContent**. innerHTML is parsing the HTML, not treating it as a string.

The easiest way to see what is going on with innerHTML in chrome is to open webdeveloper tools (f12) and look under your elements tab. This will show the generated HTML.

``<i>
</i>&lt;p id="here4"&gt;
" ping=hey I am a super text
&lt;div class="r"&gt;&lt;/div&gt;
&lt;/p&gt;<i>
</i>
``

I would guess that because the anchor tag doesn't have a closing **>** it is not being parsed.
Copy linkTweet thisAlerts:
@codewitchauthorAug 15.2020 — Thank you very much.
Copy linkTweet thisAlerts:
@daveyerwinAug 16.2020 — @rpg2009#1622138

just one more reason not to use innerHTML for adding to the DOM

innerHTML should be thought of as "read only"
Copy linkTweet thisAlerts:
@VITSUSAAug 17.2020 — @rpg2009#1622138 This is the best solution, and small mistake (syntax error) also which people do most of the time.
×

Success!

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