/    Sign up×
Community /Pin to ProfileBookmark

checkboxes with variable names

[code=html]<html>
<head>
<SCRIPT LANGUAGE=”JavaScript”>
function checkrights(userid) {
box0 = “document.form1.” + userid + “_right_0”;
box1 = “document.form1.” + userid + “_right_1”;
box2 = “document.form1.” + userid + “_right_2”;
box3 = “document.form1.” + userid + “_right_3”;
box4 = “document.form1.” + userid + “_right_4”;
box5 = “document.form1.” + userid + “_right_5”;

if (box0.checked == false) {
box0.checked = true;
}
}
</script>
</head>
<body>
<form name=”form1″>
<input type=”checkbox” name=”1_right_0″>
<input type=”checkbox” name=”1_right_1″>
<input type=”checkbox” name=”1_right_2″>
<input type=”checkbox” name=”1_right_3″>
<input type=”checkbox” name=”1_right_4″>
<input type=”checkbox” name=”1_right_5″>

<input type=”button” value=”Change” onClick=”checkrights(‘1’)”>
</form>
</body>
</html>[/code]

I don’t know much about JS, probably the answer is quite easy, but I didn’t find anything like this using search.

The purpose of this script is simplay to check the checkbox named (name=”1_right_0″). There’ll be some more checkboxes on the page, generated dynamically, the first number of the checkboxes-name will change, so I need a function where I can use a variable user-id.

But somehow using variables like here (box0 = “document.form1.” + userid + “_right_0”; ) doesn’t work.
Anyone can tell me why?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterMay 06.2007 — Bracket Notation
<i>
</i>box0=document.form1[userid+"_right_0"];

If anyone suggests eval over bracket notation in this situation, ignore them.
Copy linkTweet thisAlerts:
@GraefchenauthorMay 06.2007 — works, thx ?
×

Success!

Help @Graefchen 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.26,
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,
)...