javascript function problem

A

amli lola

Hello anyone,

this is my first encouter with javascript so please help (sorry for my bad
english, it's been a long time since high school)

i have this javascript code, and desperately trieing to write a javascript
function that will make my life much easier, the code goes like this, and I
need a javascript function so I don't have to copy/paste all the time...

Thnx..


function rezervirano()


{

// 1. condition
if(sjedala_selektirana[1])
document.A01.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[1])
{
document.A01.src ="stolica_proz_zel2.png";
}
else
document.A01.src ="stolica_proz_crv2.png";

// 2. condition
if(sjedala_selektirana[2])
document.A02.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[2])
document.A02.src ="stolica_proz_zel2.png";
else
document.A02.src ="stolica_proz_crv2.png";

// 3. condition

if(sjedala_selektirana[3])
document.A03.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[3])
document.A03.src ="stolica_proz_zel2.png";
else
document.A03.src ="stolica_proz_crv2.png";

// 4. condition
if(sjedala_selektirana[4])
document.A04.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[4])
document.A04.src ="stolica_proz_zel2.png";
else
document.A04.src ="stolica_proz_crv2.png";

// 5. condition
if(sjedala_selektirana[5])
document.A05.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[5])
document.A05.src ="stolica_proz_zel2.png";
else
document.A05.src ="stolica_proz_crv2.png";

// 6. condition
if(sjedala_selektirana[6])
document.A06.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[6])
document.A06.src ="stolica_proz_zel2.png";
else
document.A06.src ="stolica_proz_crv2.png";

// 7. condition
if(sjedala_selektirana[7])
document.A07.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[7])
document.A07.src ="stolica_proz_zel2.png";
else
document.A07.src ="stolica_proz_crv2.png";

// 8. condition
if(sjedala_selektirana[8])
document.A08.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[8])
document.A08.src ="stolica_proz_zel2.png";
else
document.A08.src ="stolica_proz_crv2.png";

// 9. condition
if(sjedala_selektirana[9])
document.A09.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[9])
document.A09.src ="stolica_proz_zel2.png";
else
document.A09.src ="stolica_proz_crv2.png";

// 10. condition
if(sjedala_selektirana[10])
document.A010.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[10])
document.A010.src ="stolica_proz_zel2.png";
else
document.A010.src ="stolica_proz_crv2.png";

// 11. condition
if(sjedala_selektirana[11])
document.A011.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[11])
document.A011.src ="stolica_proz_zel2.png";
else
document.A011.src ="stolica_proz_crv2.png";

} // function end


Thank you again...
 
L

Lasse Reichstein Nielsen

amli lola said:
i have this javascript code, and desperately trieing to write a javascript
function that will make my life much easier, the code goes like this, and I
need a javascript function so I don't have to copy/paste all the time...
function rezervirano()
// 1. condition
if(sjedala_selektirana[1])
document.A01.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[1])
{ .....
document.A011.src ="stolica_proz_crv2.png";

} // function end


Thank you again...

Is this something like what you need:

function rezerviarano() {
for(var i = 1; i <= 11; i++) {
var image = document.images['A' + lz(i)];
if (sjedala_selektirana) {
image.src = "stolica_proz_zut2.png";
} else if (sjedala_slobodna) {
image.src = "stolica_proz_zel2.png";
} else {
image.src = "stolica_proz_crv2.png";
}
}
}
function lz(n) { // prefix leading zero for numbers < 10
n = String(n);
if (n.length < 2) { n = '0' + n; }
return n;
}

I'm guessing you are running through a number of images and setting
their src-attribute based on the values of the two arrays
sjedala_selektirana and sjedala_slobodna.

/L
 
S

SAM

amli lola a écrit :
i have this javascript code, and desperately trieing to write a javascript
function that will make my life much easier, the code goes like this, and I
need a javascript function so I don't have to copy/paste all the time...

Thnx..


function rezervirano()

{

for(var i=0; i<11; i++) {
var x = i<9? '0'+(+1+i) : +1+i;
var theImg = "stolica_proz_zut2.png";
if(sjedala_selektirana) theImg = "stolica_proz_zel2.png";
else if(sjedala_slobodna) theImg = "stolica_proz_crv2.png";
document['A'+x].src = theImg;
}
}


or :

function rezervirano(num, yes, no, defaut) {
for(var i=0; i<num; i++) {
var theImg = defaut,
x = +1+i;
x = x<9? '0'+x : x;
if(sjedala_selektirana) theImg = yes;
else if(sjedala_slobodna) theImg = no;
document['A'+x].src = theImg;
}
}

onclick="rezervirano( 11,
'stolica_proz_zel2.png',
'stolica_proz_crv2.png',
'stolica_proz_zut2.png' );"
 
E

Evertjan.

SAM wrote on 14 feb 2008 in comp.lang.javascript:
var x = i<9? '0'+(+1+i) : +1+i;
x = +1+i; x = x<9? '0'+x : x;

Why +1+i ?

In spite of the effort,
that won't work if i is a string,
see:

var i='8'; alert( +1+i ); // returns 18

now try:

var i='8'; alert( +i+1 ); // returns 9

=============

I would prefer adding to x, rather that to i:

var x = i; x = (++x>9) ? x : '0'+x;

or

var x = i; if (++x<10) x = '0'+x;
 
D

Dr J R Stockton

Wed said:
function lz(n) { // prefix leading zero for numbers < 10
n = String(n);
if (n.length < 2) { n = '0' + n; }
return n;
}

That, tested in <URL:http://www.merlyn.demon.co.uk/js-date9.htm#LZS>
seems equivalent, except for non-integers, to

function LZ(n) { return (n!=null&&n<10&&n>=0?"0":"") + n }

but a little slower. Approaches unlike yours need to be carefully done
if they are to correctly handle all non-fractional inputs which are not
positive, such as NaN & null.

function XXX(n) { return (n=String(n)).length < 2 ? '0'+n : n }

seems marginally faster, in IE6, than the above lz.
 
S

SAM

Evertjan. a écrit :
SAM wrote on 14 feb 2008 in comp.lang.javascript:



Why +1+i ?

In spite of the effort,
that won't work if i is a string,

I had no error in my tests but of course, as usually, you're right.
I would prefer adding to x, rather that to i:

var x = i; x = (++x>9) ? x : '0'+x;

oh yes !
 
A

amli lola

amli lola said:
Hello anyone,

this is my first encouter with javascript so please help (sorry for my bad
english, it's been a long time since high school)

i have this javascript code, and desperately trieing to write a javascript
function that will make my life much easier, the code goes like this, and
I need a javascript function so I don't have to copy/paste all the time...

Thnx..


function rezervirano()


{

// 1. condition
if(sjedala_selektirana[1])
document.A01.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[1])
{
document.A01.src ="stolica_proz_zel2.png";
}
else
document.A01.src ="stolica_proz_crv2.png";

// 2. condition
if(sjedala_selektirana[2])
document.A02.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[2])
document.A02.src ="stolica_proz_zel2.png";
else
document.A02.src ="stolica_proz_crv2.png";

// 3. condition

if(sjedala_selektirana[3])
document.A03.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[3])
document.A03.src ="stolica_proz_zel2.png";
else
document.A03.src ="stolica_proz_crv2.png";

// 4. condition
if(sjedala_selektirana[4])
document.A04.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[4])
document.A04.src ="stolica_proz_zel2.png";
else
document.A04.src ="stolica_proz_crv2.png";

// 5. condition
if(sjedala_selektirana[5])
document.A05.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[5])
document.A05.src ="stolica_proz_zel2.png";
else
document.A05.src ="stolica_proz_crv2.png";

// 6. condition
if(sjedala_selektirana[6])
document.A06.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[6])
document.A06.src ="stolica_proz_zel2.png";
else
document.A06.src ="stolica_proz_crv2.png";

// 7. condition
if(sjedala_selektirana[7])
document.A07.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[7])
document.A07.src ="stolica_proz_zel2.png";
else
document.A07.src ="stolica_proz_crv2.png";

// 8. condition
if(sjedala_selektirana[8])
document.A08.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[8])
document.A08.src ="stolica_proz_zel2.png";
else
document.A08.src ="stolica_proz_crv2.png";

// 9. condition
if(sjedala_selektirana[9])
document.A09.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[9])
document.A09.src ="stolica_proz_zel2.png";
else
document.A09.src ="stolica_proz_crv2.png";

// 10. condition
if(sjedala_selektirana[10])
document.A010.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[10])
document.A010.src ="stolica_proz_zel2.png";
else
document.A010.src ="stolica_proz_crv2.png";

// 11. condition
if(sjedala_selektirana[11])
document.A011.src ="stolica_proz_zut2.png";
else if(sjedala_slobodna[11])
document.A011.src ="stolica_proz_zel2.png";
else
document.A011.src ="stolica_proz_crv2.png";

} // function end


Thank you again...
Thanks Lasse, worked perfect...
Now, I have another problem, I've posted it as a new message...

Thanks again...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top