/    Sign up×
Community /Pin to ProfileBookmark

Hey,

I’m trying to learn programming in JS by making some programs.

I have made a form:

<form name=”form” action=”>
<div class=”row”><input type=’checkbox’ id=’checkbox0′ name=’checkbox0′ value=’1′ /> Test 01</div>
<div class=”row”><input type=’checkbox’ id=’checkbox1′ name=’checkbox1′ value=’2′ /> Test 02</div>
<div class=”row”><input type=’checkbox’ id=’checkbox2′ name=’checkbox2′ value=’3′ /> Test 03</div>
<div class=”row”><input type=’checkbox’ id=’checkbox3′ name=’checkbox3′ value=’4′ /> Test 04</div>
<div class=”end”>
<button type=’submit’ name=”save”>Selectie opslaan</button>
<input type=”hidden” id=”aantal_domeinen” name=”aantal_domeinen” value=”{$aantal_domeinen}” />
</div>
</form>

with the next js code I will check which checkbox is selected:

var nameField = ”;
var nameFieldString = ”;

for (i=0; i<document.getElementById(“aantal_domeinen”).value; i++){
nameField = ‘checkgroup’+i;
nameFieldString = nameField.toString();
alert(“Check = “+nameFieldString);

if (nameFieldString.checked==true ){
alert(“This item is selected = “+i);
}
}

The code ‘if (nameFieldString.checked==true ){‘ is not working ?. Who can tell me what I’m doing wrong.

Thx

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameJun 13.2011 — A string does not have a 'checked' property, also you use 'checkgroup'+i but there is no element shown with name or id starting with 'checkgroup', rather 'checkbox'+digit.

Also, form field values are seen as strings so if using aantal_domeinen it's value should be converted to number via parseInt. Though I wouldn't use aantal_domeinen, I would just loop until can't find an element with the id and then break out of loop.
×

Success!

Help @Rayen_Developer 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.17,
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,
)...