logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > Javascript and JS frameworks
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 08-25-2011, 05:48 AM   #1
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default for() in javascript

searched web and cant find which way to go with this. I am trying to amend a date timepicker calender.

the function contains two incemented values for hour and minute. i alterd the values of the hours to suitable opening hours. but in lost with minutes. this is also a for() loop using i++, but what i would like is 2 values ie on the hour and 30 mins. so can i specify the increment for i , or would this need re writing (while loop?)
cant find where to look for clues . the relevant part of the method

Code:
var selectContainer = new Element('div', {className: 'selectContainer'});
		this.timeSelector.appendChild(selectContainer);		
		selectContainer.appendChild(new Element('div').update('Select time:'));		
		this.hours = new Element('select');
		this.minutes = new Element('select');
		
		for (var i=9; i<17; i++) {
			this.hours.appendChild(new Element('option').update(i < 10 ? '0' + i : i));  
		} 
		
		for (var i=0; i<60; i++) {
			this.minutes.appendChild(new Element('option').update(i < 10 ? '0' + i : i));

		} 
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 08-25-2011, 06:25 AM   #2
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

found it javascriptkit.com tutorials

Code:
for (var i=30; i<=60; i=i+30) {
			this.minutes.appendChild(new Element('option').update(i < 10 ? '0' + i : i));
sorry javascript i tke back all those badnames i called you
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 08-25-2011, 08:26 PM   #3
ranjan
 
Join Date: Dec 2004
Posts: 405
Default Just FYI

Consider this part in your code

Code:
i < 10 ? '0' + i : i
It is basically a shortcut if statement that says if i is less than 10 prepend i with a 0 else, just let i be i

so the code makes 1 = 01, 2 = 02 and so on but lets 10 = 10, 11=11 and so on..

Now look at your for initialization and conditions

Code:
var i=30; i<=60; i=i+30
if you analyze the code you have only 2 possible values of i, 30 and 60, never less than 10 and so the first bit of code (i < 10 ? '0' + i : i) is unnecessary!

So this line

Code:
this.minutes.appendChild(new Element('option').update(i < 10 ? '0' + i : i));
can be safely be changed to

Code:
this.minutes.appendChild(new Element('option').update(i));
ranjan is offline   Reply With Quote
Old 08-26-2011, 12:50 AM   #4
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

thanks ranjan i had wondered about that part.
i had lost my nerve a bit as id had a few stab in the dark attempts which as a fauly script managed to freeze firefox and in turn my pc so i was getting abit rattled
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:33 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com