/    Sign up×
Community /Pin to ProfileBookmark

getElementById to change variables (or something)

here’s my ultimate goal:

random band name generator!– the visitor picks 3 numbers 1-100 or clicks random to have them picked for him and then the three numbers refer to three seperate lists of 100 words to produce the result of an nearly endless possible outcomes of unique phrases

here’s my problem:
I can’t figure out how to get the values of the three number boxes to update the numbers for the varibles of the three lists. actually i can’t get the values of the three number boxes to do much of anything.

This is my first attempt at doing anything with javascript, and I don’t really know what I’m doing, but here’s what I’ve tried so far:
first of all, I haven’t actually gotten to the afformentioned lists of awesome words yet. I’ve just been trying to get the values of the three boxes to affect the text at all. my most recent failed attempt was

[code=php]<head>
<script language=”javascript”>
function Process1() {
var str=document.getElementById(‘list1’).value;
document.getElementById(‘word1’).innerHTML=str;
}

function Process2() {
var str=document.getElementById(‘list2’).value;
document.getElementById(‘word2’).innerHTML=str;
}

function Process3() {
var str=document.getElementById(‘list3’).value;
document.getElementById(‘word3’).innerHTML=str;
}
</script></head>

<p>
<form name=form>
<label for=”list1″><input type=text name=text1 id=”text1″ size=3 maxlength=3></label>
<label for=”list2″><input type=text name=text2 id=”text2″ size=3 maxlength=3></label>
<label for=”list3″><input type=text name=text3 id=”text3″ size=3 maxlength=3></label><br>
<input type=button value=”roll the dice” name=button onclick=”random1(numb1, dice1); random2(numb2, dice2); random3(numb3,

dice3)”><input type=button value=”generate” onclick=”Process1(); Process2(); Process3()”>
</form>
</p>
<p id=”word1″>first word</p>
<p id=”word2″>second word</p>
<p id=”word3″>third word</p>[/code]

i also tried a function that was something to the efect of

if document.form.text1.value=1
document.write(“word1”)
else if document.form.text1.value=2
document.write(“word2”)
etc, etc,
…but that wasn’t doing anything

it seems like it should be a pretty basic function, but I just can’t seem to figure it out. can someone give me a push in the right direction?

here’s the whole thing in case anyone cares

[code=php]<html>
<head>
<script language=”javascript”>
<!–
var numb1 = 100;
var numb2 = 100;
var numb3 = 100;

var dice1 = 1;
var dice2 = 1;
var dice3 = 1;

function random1 (numb1, dice1) {
var roll1 = 0;
for (loop=0; loop < dice1; loop++) {

roll1 = roll1 + Math.round(Math.random() * numb1) % numb1 + 1;
}
document.form.text1.value = roll1;
}
function random2 (numb2, dice2) {
var roll2 = 0;
for (loop=0; loop < dice2; loop++) {

roll2 = roll2 + Math.round(Math.random() * numb2) % numb2 + 1;
}
document.form.text2.value = roll2;
}

function random3 (numb3, dice3) {
var roll3 = 0;
for (loop=0; loop < dice3; loop++) {

roll3 = roll3 + Math.round(Math.random() * numb3) % numb3 + 1;
}
document.form.text3.value = roll3;
}

function Process1() {
var str=document.getElementById(‘list1’).value;
document.getElementById(‘word1’).innerHTML=str;
}

function Process2() {
var str=document.getElementById(‘list2’).value;
document.getElementById(‘word2’).innerHTML=str;
}

function Process3() {
var str=document.getElementById(‘list3’).value;
document.getElementById(‘word3’).innerHTML=str;
}

//–>
</script>
</head>

<body bgcolor=”#000000″ text=”#FFFFCC”>

<basefont face=”arial”>

Enter 3 numbers 1-100:<br>
<p>
<form name=form>
<label for=”list1″><input type=text name=text1 id=”text1″ size=3 maxlength=3></label>
<label for=”list2″><input type=text name=text2 id=”text2″ size=3 maxlength=3></label>
<label for=”list3″><input type=text name=text3 id=”text3″ size=3 maxlength=3></label><br>
<input type=button value=”roll the dice” name=button onclick=”random1(numb1, dice1); random2(numb2, dice2); random3(numb3,

dice3)”><input type=button value=”generate” onclick=”Process1(); Process2(); Process3()”>
</form>
</p>
<p id=”word1″>first word</p>
<p id=”word2″>second word</p>
<p id=”word3″>third word</p>

</body>
</html>[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 19.2006 — First at all... You may use a single function (not 3). Use arrays to set your variables.
Copy linkTweet thisAlerts:
@antisexyauthorJan 19.2006 — well then how do i get it to do it three times.. and what's an array?
Copy linkTweet thisAlerts:
@KorJan 19.2006 — My oh my... You don'y know what array is....

http://jennifermadden.com/javascript/arraySimple.html

or google for

Here's an example of what you should have done (if I understood well your aim):
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var lists=[];//THIS IS AN ARRAY!!!!!!!!!!!!!!
lists[0] =['lorem','ipsum','dolor','sit','amet'];//insert your words here till 100
lists[1] =['nullam','nonummy','purus','porta','nibh'];//insert your words here till 100
lists[2] =['vivamus','consectetuer','sapien','vel','ipsum'];//insert your words here till 100
var f;var t;
function randN(){
for(var i=0;i<lists.length;i++){f[i].value=Math.floor(Math.random()*lists[i].length)+1}
}
function showR(){
var sentence='';var q=1;
for(var i=0;i<lists.length;i++){
var v = f[i].value;
sentence=(v.length>0&&Number(v)&&Number(v)>0&&Number(v)<=lists[i].length)?sentence+' '+lists[i][Number(v-1)]:q=0;
}
Boolean(q)?t.firstChild.data=sentence:alert('Invalid input! Enter correct numbers');
}
onload = function(){
f=document.getElementById('cont').getElementsByTagName('input');
t=document.getElementById('txt')
}
</script>
</head>
<body>
input in each field a number between 1-5
<div id="cont">
<input type="text"><input type="text"><input type="text">
</div>
Or let the dice to do that for you :<br>
<input type="button" value="Random generate" onclick="randN()"><br>
<br>
<input type="button" value="Show the result" onclick="showR()">
<br>
<div id="txt">&nbsp;</div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@antisexyauthorJan 19.2006 — thanks a lot,

although, for the sake of learning something, i wish i understood it more.... i get the arrays, but the part that i was trying to figure out still dosen't make any sense:
[code=php]function showR(){
var sentence='';var q=1;
for(var i=0;i<lists.length;i++){
var v = f[i].value;
sentence=(v.length>0&&Number(v)&&Number(v)>0&&Number(v)<=lists[i].length)?sentence+' '+lists[i][Number(v-1)]:q=0;
}
Boolean(q)?t.firstChild.data=sentence:alert('Invalid input! Enter correct numbers');
} [/code]


what's "boolean(q)"?

and what's "firstChild.data"?

and is "sentence" creating the string of three words? how does "&nbsp;" refer to that?

oh well, anyway thanks a lot... it is exactly what I was looking for. [URL=http://www.gizburgduck.com/toys/bandname.html]check it out[/URL]. although it's still not done... it was my friends idea, i put it together for him, and now it's up to him to come up with 230 more words.. but the 18 i already got are hillarious
Copy linkTweet thisAlerts:
@UltimaterJan 19.2006 — [color=blue]Boolean[/color] is a function(method) that accepts a single argument(parameter) and evaluates the argument as a boolean value and returns either [color=blue]true[/color] or [color=blue]false[/color] depending.
<i>
</i>&lt;script type="text/javascript"&gt;
alert(Boolean(2))//true
alert(Boolean(1))//true
alert(Boolean(0))//false
alert(Boolean(null))//false
&lt;/script&gt;


To give you an idea what the Boolean function would look like IF it was user defined (but it's built-in):
<i>
</i>function Boolean(a){

if(a){
return true;
}
else{
return false;
}

}




Moving on....



[color=blue]firstChild.data[/color] is bascially the same thing as [color=blue]innerHTML[/color] however limited because you cannot use HTML tags (if you try to, it will just treat the tag as regular text). It is used because [color=blue]innerHTML[/color] is non-standard meaning that new browsers aren't required to support it and therefore there are many browsers that don't understand it.


Futhermore...

<i>
</i>sentence=(v.length&gt;0&amp;&amp;Number(v)&amp;&amp;Number(v)&gt;0&amp;&amp;Number(v)&lt;=lists[i].length)?sentence+' '+lists[i][Number(v-1)]:q=0;

can be written as:
<i>
</i> if(v.length&gt;0 &amp;&amp; Number(v) &amp;&amp; Number(v)&gt;0 &amp;&amp; Number(v)&lt;=lists[i].length){
sentence = sentence + ' ' + lists[i][Number(v-1)];
}
else{
sentence=0;
q=0;
}


The [color=blue]?:[/color] opperators, when combined, basically act as a short if-else statment.
×

Success!

Help @antisexy 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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