Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>Parameter Used to Update Formula

Parameter Used to Update Formula

swtrans
03-16-2006, 03:35 PM
If this was working correctly the alert box's value would equal 1.

Any help would be appreciated. Thanks!!

------------------------------------

<script type="text/javascript">

var answer01 = 0;
var answer02 = 0;

function show(obj)
{
answer(obj) = 1;
}

function doit(param)
{
show("+param+");
alert(answer01);
}
</script>
<input type="button" value="Answer01" onclick="doit('01')"><br><br>
<input type="button" value="Answer02" onclick="doit('02')">

Beagle
03-16-2006, 03:59 PM
var answer01 = 0;
var answer02 = 0;

function show(obj)
{
answer(obj) = 1;
}

I think you want an array:


var answers = new Array();

function show(obj)
{
answers[obj] = 1;
}

function doit(param)
{
show("+param+");
alert(answer['01']);
}

swtrans
03-16-2006, 04:13 PM
I'll try messing with an array. Is there any way to do this without an array?

Kor
03-16-2006, 05:36 PM
yes

window['answer'+obj]=1;

following the fact that variables and functions are "attached" to the window object.

swtrans
03-16-2006, 06:07 PM
Kor, am I doing this right? Thanks!

<script type="text/javascript">

var answer01 = 0;
var answer02 = 0;

function show(obj)
{
window['answer'+obj]=1;
}

function doit(param)
{
show("+param+");
alert(answer01);
}
</script>
<input type="button" value="Answer01" onclick="doit('01')"><br><br>
<input type="button" value="Answer02" onclick="doit('02')">

Kor
03-16-2006, 06:35 PM
Do you need 2 functions by all mean?

...
var answer01 = 0;
var answer02 = 0;
function doIt(q){
window['answer'+q]=1;
alert(window['answer'+q])
}
...


This way you modify dynamically the value of the variables answer01,answer02...and so on

In fact you may use the window[something] notation even to set your initial values.

var nr =15;//nr of answers
for(var i=0;i<nr;i++){
var q=((i+1)<10)?'0'+(i+1):i+1;
window['answer'+q]=0;
}
function doIt(q){
window['answer'+q]=1;
alert(window['answer'+q])
}

Kor
03-16-2006, 06:38 PM
But in fact it is not much different by using arrays. Pretty much the same.

Kor
03-16-2006, 08:55 PM
I see now that you don't actually need to set the variables. You may use the existence/nonexistence of the variables and transform that Boolean into 0 or 1.

Is this code close to your needs?

<!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 a='Answer'
var len;
function doIt(q){
window[a+q]=1;
writeRes()
}
function writeRes(){
var t=''
for(var i=0;i<len;i++){
var p=((i+1)<10)?'0'+(i+1):i+1;
t+=a+p+' ='+Number(Boolean(window[a+p]))+'<br>';
}
t=t.replace(/<br>$/,'');
document.getElementById('res').innerHTML=t;
}
onload = function(){
var but= document.getElementById('ans').getElementsByTagName('input');
len=but.length;
for(var i=0;i<len;i++){
but[i].b=((i+1)<10)?'0'+(i+1):i+1;
but[i].onclick=function(){doIt(this.b)}
}
}
</script>
</head>
<body>
<div id="ans">
<input type="button" value="Answer01"><br><br>
<input type="button" value="Answer02"><br><br>
<input type="button" value="Answer03"><br><br>
<input type="button" value="Answer04"><br><br>
<input type="button" value="Answer05">
</div>
<div id="res">&nbsp;</div>
</body>
</html>

-----
In fact... can you go further and detail your aim? It lookks like you need to check the true/false of some choices. Maybe we could find even a shorter way to do that.


 

TOP

Hyperthreading hurts s
HP sharpens blade PC l
Apple goes for Quad po
Itanium gets scaled do

For more info

HP sharpens blade PC l
Hyperthreading hurts s
can not call click() i
script does not work o
smart menu 
write to an html file 
FireFox doesn't recogn
Changing e.keyCode (e.
Clearing a range creat
UPDATE: My mouse over 

News Archive

onChange Help 
beforeload? vs onload.
Problem with document.
decreasing my javascri
Problem with \\ and \ 
how to stop scrolling?
Change Label Text Colo
Can I forward a page b
2 javascript functions
Validating a textbox 

Related stories:

Hiding an email address using javascript
Dropdown works in IE / Firefox but not Safari
Styling disabled form elements?
Dynamically Populate Slideshow
when to use return functionName on events
script that adds item to cart without going to shopping cart
problem with HTTPS
ebaY Category Picker doesn't work
window.opener problem when navigating away

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics