/    Sign up×
Community /Pin to ProfileBookmark

Problems with "if" statement

<script>

function lala() {

for (i=0;i<5;i++) {

if (document.forma.elements[“pole[“+i+”]”].value == “1”){

document.forma.elements[“pole[“+i+”]”].value = “Slava”

}

}

}

</script>

<form name=forma>

<input type=text name=pole[1] value=”1″>

<input type=button onclick=”lala()”>

</form>

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

Internet Explorer shows error “document.forma.elements[…].value is not an object” and doesn’t do anything

if I replace “<input type=text name=pole[[b]1[/b]] value=”1″>” on “<input type=text name=pole[[b]0][/b]] value=”1″>”, script will work, but still displays error.

Why is it? How to fix&

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SmittyMay 30.2004 — Congratulations, your IF statment does work!

Its your html code thats causing the error.

(i=0;i<5;i++)

goes from 0 -> 4

but you start at pole[1], so its crashing when its trying to find pole[0].

When u change the name to pole[0], the loop then try's to find pole[1],pole[2] ,pole[3] ,pole[4]

Have a crack at tihs below. I haven't changed the If statment, just the html its accessing.


**********************************************************************
<script>

function lala()

{

for (i=0;i<5;i++)

if (document.forma.elements["pole["+i+"]"].value == "1")

document.forma.elements["pole["+i+"]"].value = "Slava"

}

</script>

<form name=forma>

<input type=text name=pole[0] value="0"><br>

<input type=text name=pole[1] value="1"><br>

<input type=text name=pole[2] value="2"><br>

<input type=text name=pole[3] value="3"><br>

<input type=text name=pole[4] value="4"><br>

<input type=button onclick="lala()" value="lala()">

</form>
×

Success!

Help @Skyer 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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