/    Sign up×
Community /Pin to ProfileBookmark

Looking for a simple Loop to end of Select

Okay no secret, my Javascript ability is lack luster at best.

First off I’m not sure if I need a loop or some code to set the index value..

I have 2 secondary select boxs that are populated by another master select box in PHP.

This new select boxes will only have 2 values, the first is default, the second is the populated data.

When the master select box is changed a javascript function fires to reload the page with the php selected data. The problem is that the I don’t know how to change the selected index of the second boxes to show the retrieved data.

an example of this is seen here: [url]http://www.onlinecdkey.com/orings.php?cat=Fluorocarbon%20%28Brown%29&measure=AS568&duro=75&as568=005[/url]
Please look at the Cross Section and Inside Diameter select boxes. You should see data there..

What I need to do is either loop to the end of options, or just add +1 to the selected index since I know the returned results will always be 1 record.

This is my reload function:

[CODE]function reload7(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.measure.options[form.measure.options.selectedIndex].value;
var val3=form.as568.options[form.as568.options.selectedIndex].value;
var val5=form.duro.options[form.duro.options.selectedIndex].value;
self.location=’orings.php?cat=’ + val + ‘&measure=’ + val2 + ‘&duro=’ + val5 + ‘&as568=’ + val3 ;
}[/CODE]

My Cross Section select is named as_cs and my inside diameter is named as_id

I tried adding
form.as_cs.options[form.as_cs.options.selectedIndex].value +1;
and
form.as_cs.options[form.as_cs.options.selectedIndex].value ++;
to the end of the function but that didn’t work.

I assume this is simple, but I’m having trouble finding any examples of some one doing this.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@toicontienJun 26.2010 — Form field values are always strings, and using the + operator concatenates two strings together. Instead, you want to convert the string field value to a number:

[CODE]var num = Number( form.as_cs.options[form.as_cs.options.selectedIndex].value ) + 1;[/CODE]

[B]Additional Info: A Practical Guide To Numbers in JavaScript.[/B]
Copy linkTweet thisAlerts:
@dfordauthorJun 26.2010 — Thank you very much for the replay.

*note I changed "form" to "f1" which is the actual name of the form with the elements.

I tried this and kathousand other similar things:
[CODE]function reload7(form)
{
var val=f1.cat.options[f1.cat.options.selectedIndex].value;
var val2=f1.measure.options[f1.measure.options.selectedIndex].value;
var val3=f1.as568.options[f1.as568.options.selectedIndex].value;
var val5=f1.duro.options[f1.duro.options.selectedIndex].value;
var num=Number(f1.as_cs.options[f1.as_cs.options.selectedIndex].value )+1;
var num2=Number(f1.as_id.options[f1.as_id.options.selectedIndex].value )+1;
self.location='orings.php?cat=' + val + '&measure=' + val2 + '&duro=' + val5 + '&as568=' + val3 ;
}[/CODE]


At best my efforts didn't work, at worst they stopped the reload function all together.

Could there be some issue with the fact that data is getting pulled from a database and when this fires the data hasn't been loaded yet? I really am stumped and don't know what else to try.
×

Success!

Help @dford 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.3,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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