/    Sign up×
Community /Pin to ProfileBookmark

help with a loop please

I have the following

function Fchanger() {

if(document.forms[0].state1.value.length > 0)
document.forms[0].state1.style.borderColor=’#000000′;
if(document.forms[0].state1.value.length > 0)
document.forms[0].state1.style.borderWidth=’1px’;
if(document.forms[0].state1.value.length > 0)
document.forms[0].state1.style.borderStyle=’solid’;

}

I’d rather not write this out for all 50 states (6 lines x 50 states =300 lines !). Is there a way I can “loop” this to cover all the states and keep the code manageable?

Thanks,
Ron

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@PittimannDec 04.2003 — Hi!

Try something like this:
[code=php]
<script type="text/javascript">
<!--
function Fchanger() {
//all states have to go into the array like "Name1","Name2"...
allStates = new Array("Alabama","Hawaii","Michigan","Virginia","Washington");
for (var i = 0; i < allStates.length; i++) {
if (document.myForm[allStates[i]].value.length >0){
document.myForm[allStates[i]].style.borderColor='#000000';
document.myForm[allStates[i]].style.borderWidth='1px';
document.myForm[allStates[i]].style.borderStyle='solid';
}
}
}
//-->
</script>
[/code]

By the way (1): it is not necessary to repeat the same if condition 3 times in your example. That would do as well:

function Fchanger() {

if(document.forms[0].state1.value.length > 0) {

document.forms[0].state1.style.borderColor='#000000';

document.forms[0].state1.style.borderWidth='1px';

document.forms[0].state1.style.borderStyle='solid';

}

}

By the way (2): in CSS you could also define two classes for the form elements. One is assigned in the hard code and another one with your three style attributes which you assign in your function. Then you would just have to change the class...

Cheers - Pit
Copy linkTweet thisAlerts:
@batfinkDec 04.2003 — If each element had an id it would be even easier.

function Fchanger()

{

a=document;

for(i=1;i<51;i++)

{

if(a.getElementById("state" + i).value.length > 0)

{

a.getElementById("state" + i).style.borderColor='#000000';

a.getElementById("state" + i).style.borderWidth='1px';

a.getElementById("state" + i).style.borderStyle='solid';

}

}

}
Copy linkTweet thisAlerts:
@rjusaauthorDec 04.2003 — Hey Pittimann,

Thanks alot for your help. I constructed the 50 state array and used your code, even renamed the form myForm. When I go to run the script I get

'doc.myForm[...].value' is null or not an object...

What'd I screw up?

Thanks again,

Ron
Copy linkTweet thisAlerts:
@PittimannDec 04.2003 — Hi!

Sorry - I thought, I posted the form as well. I didn't (f..k)!

the names of the formfields have to be the names in the array, but you should really consider batfinks proposal.

Well - to get "my" stuff work, just name the formfields and the stuff will work.

Sorry again for not pasting the form into my post last time.

Cheers - Pit
×

Success!

Help @rjusa 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.25,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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