/    Sign up×
Community /Pin to ProfileBookmark

Need Help with script for 2 variables

I have started the base of the code where it prompts the user for a list of options on 3 different types of lists. Now I need to add a second prompt (window) to ask the user to choose between 3 different colors for the text. I’m new to javascripting, so I’m a bit lost at the moment. Any help would be greatly appreciated. Below is the start of my script:

[code]
<?xml version = “1.0”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns = “http://www.w3.org/1999/xhtml”>
<head>
<title>Switching between XHTML List Formats</title>

<script type = “text/javascript”>
<!–
var choice, // user’s choice
startTag, // starting list item tag
endTag, // ending list item tag
validInput = true, // indicates if input is valid
listType; // list type as a string

choice = window.prompt( “Select a list style:n” +
“1 (bullet), 2 (numbered), 3 (lettered)”, “1” );

switch ( choice ) {
case “1”:
startTag = “<ul>”;
endTag = “</ul>”;
listType = “<h1>Bullet List</h1>”;
break;
case “2”:
startTag = “<ol>”;
endTag = “</ol>”;
listType = “<h1>Ordered List: Numbered</h1>”;
break;
case “3”:
startTag = “<ol type = “A”>”;
endTag = “</ol>”;
listType = “<h1>Ordered List: Lettered</h1>”;
break;
default:
validInput = false;
}

if ( validInput == true ) {
document.writeln( listType + startTag );

for ( var i = 1; i <= 3; ++i )
document.writeln( “<li>List item ” + i + “</li>” );

document.writeln( endTag );
}
else
document.writeln( “Invalid choice: ” + choice );
// –>
</script>

</head>
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
</html>
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 13.2007 — I would have used DOM to keep the same session... I don't know why you need XHTML doctype, but if so, you have to isolate the javascript code inside a CDATA island:
<i>
</i>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;script type="text/javascript"&gt;
/*&lt;![CDATA[*/
var tags=[
['UL','Bullet List'],
['OL','Ordered List: Numbered'],
['OL','Ordered List: Lettered']
];
var list=[
'apples',
'bananas',
'oranges',
'lemons'
]
var colors=[
'red',
'green',
'blue'
]
function chooseList(){
var choice = window.prompt('Select a list style:n1 (bullet), 2 (numbered), 3 (lettered)','1');
while(!tags[Number(choice)-1]){
choice = window.prompt('Invalid Choice! Select a list style:n1 (bullet), 2 (numbered), 3 (lettered)','1')
}
var col = window.prompt('Select a color:n1 (red), 2 (green), 3 (blue)','1');
while(!tags[Number(choice)-1]){
col = window.prompt('Invalid Choice! Select a color:n1 (red), 2 (green), 3 (blue)','1')
}
var oH, oO, oL, root=document.getElementById('mydiv');
oH=document.createElement('h1');
oH.appendChild(document.createTextNode(tags[Number(choice)-1][1]));
oH.style.backgroundColor=colors[Number(col)-1]
root.appendChild(oH);
oO=document.createElement(tags[Number(choice)-1][0])
Number(choice)-1==2?oO.type='A':null
for(var j=0;j&lt;list.length;j++){
oL=document.createElement('li');
oL.appendChild(document.createTextNode(list[j]));
oL.style.backgroundColor=colors[Number(col)-1]
oO.appendChild(oL);
}
root.appendChild(oO);
}
function runAgain(){
var root=document.getElementById('mydiv');
while(root.hasChildNodes()){
root.removeChild(root.childNodes[0])
}
chooseList();
}
onload=chooseList;
/*]]&gt;*/
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="mydiv"&gt;&lt;/div&gt;
&lt;div style="cursor:pointer" onclick="runAgain()"&gt;&lt;br /&gt;Click here to run the script again&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

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