/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Separating JS from HTML

I’m trying to separate the HTML from the JS function, but having not much luck.
Any ideas why I can not accomplish this?

[code]
<!DOCTYPE HTML>
<html>
<head>
<title> FAQ display</title>
<meta charset=”utf-8″>

<style type=”text/css”>
dt { line-height: 1.5em; }
</style>

</head>
<body>

<h1> FAQ List </h1>

<dl id=”FAQlist”>
<dt>faq #1</dt>
<dd>Answer to FAQ #1</dd>
<dt>faq #2</dt>
<dd>Answer to FAQ #2</dd>
<dt>faq #3</dt>
<dd>Answer to FAQ #3</dd>
<dt>More faqs </dt>
<dd>Answers to additional FAQs</dd>
<!– other Questions and Answers follow –>
</dl>

<script type=”text/javascript”>
function toggleDD(IDS) {
var tmp = document.getElementById(IDS);
if (tmp.style.display != ‘block’) { tmp.style.display = ‘block’; }
else { tmp.style.display = ‘none’; }
}
window.onload = function() {
// set-up actions of DL, DT and DD display
var sel = document.getElementById(‘FAQlist’).getElementsByTagName(‘dt’);
for (var i=0; i<sel.length; i++) {
sel[i].onclick=”toggleDD(‘faq”+i+”‘)”;
sel[i].style.cursor= ‘pointer’;
}

sel = document.getElementById(‘FAQlist’).getElementsByTagName(‘dd’);
for (var i=0; i<sel.length; i++) {
sel[i].id = ‘faq’+i;
sel[i].style.display = ‘none’;
}
// dd and dt must match in pairings of dl list
}
</script>
</body>
</html>
[/code]

I’m not getting any errors, but then it doesn’t do what I expect anyway ???
Expect to toggle display for FAQ answers based upon users click.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PadonakMay 06.2013 — put the script in the head section like all the good boyz do and change it a little:

<i>
</i>&lt;/style&gt;
&lt;script type="text/javascript"&gt;
function toggleDD(IDS) {
var tmp = document.getElementById(IDS);
if (tmp.style.display != 'block') { tmp.style.display = 'block'; }
else { tmp.style.display = 'none'; }
}
window.onload = function() {
// set-up actions of DL, DT and DD display
var sel = document.getElementById('FAQlist').getElementsByTagName('dt');
for (var i=0; i&lt;sel.length; i++){
sel[i].id='dt'+i;
sel[i].onclick=function(){toggleDD('faq'+this.id.substring(2,this.id.length));}
}

sel = document.getElementById('FAQlist').getElementsByTagName('dd');
for (var i=0; i&lt;sel.length; i++) {
sel[i].id='faq'+i;
sel[i].style.display = 'none';
sel[i].style.cursor= 'pointer';
}
// dd and dt must match in pairings of dl list
}
&lt;/script&gt;
&lt;/head&gt;
Copy linkTweet thisAlerts:
@JMRKERauthorMay 06.2013 — put the script in the head section[/QUOTE]

That did nothing. Same results.

I experimented some more and came up with this solution. Just alter the SCRIPT section to...
<i>
</i>&lt;script type="text/javascript"&gt;
function toggleDD(IDS) {
var tmp = document.getElementById(IDS); <br/>
if (tmp.style.display != 'block') { tmp.style.display = 'block'; }
else { tmp.style.display = 'none'; }
}
window.onload = function() {
// set-up actions of DL, DT and DD display
var sel = document.getElementById('FAQlist').getElementsByTagName('dt');
for (var i=0; i&lt;sel.length; i++) {
sel[i].id= 'faq'+i;
sel[i].onclick= function () { toggleDD('_'+this.id); }
sel[i].style.cursor= 'pointer';
}

sel = document.getElementById('FAQlist').getElementsByTagName('dd');
for (var i=0; i&lt;sel.length; i++) {
sel[i].id = '_faq'+i;
sel[i].style.display = 'none';
}
// dd and dt must match in pairings of dl display
}
&lt;/script&gt;

All else is the same with script just before the </body> tag.
Copy linkTweet thisAlerts:
@PadonakMay 06.2013 — read again my prev post pls
Copy linkTweet thisAlerts:
@JMRKERauthorMay 06.2013 — read again my prev post pls[/QUOTE]

Thanks, that worked too.

I did not see your code in your posting #2 (perhaps you added it after my post #3 ???)

or I must have done something wrong when I copied/pasted from my original post.

Either way, now I have two versions that do the same thing. ?
Copy linkTweet thisAlerts:
@PadonakMay 06.2013 — i have internet access through a 3G modem and sometimes some parts of my posts get lost. i noticed that the post #2 is incorrect and reposted the full version
×

Success!

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