Jump to content

Validating dynamic checkboxes


ShaunP1989

Recommended Posts

Hi there I am kind of new to javascript programming and Im having an error with some code. Firstly I output checkboxes, using php, checkboxes are outputed according to what data is in the database. Each checkbox is given a unique name, chkP[count], where count is a numerical value depending on how many checkboxes have been outputed, the first one therefore being called chkP0, followed by chkP1 etc. I then store the total value of count in a hidden field. In my testing it is 7. The problem comes when I try to recover this value in javascript.

 

The html code for the hidden field is:

 

<input type="hidden" name="hidpcount" id="pcount" value="7">

 

I then get the value into javascript using the following javascript code:

 

element = document.getElementById('pcount');

pcount = element.value;

pcount--;

 

Once this code has been executed the variable pcount holds 6.

 

The problem I get is when i try to use this variable in a for loop:

 

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

{

checkbox = "chkP" + i;

element = document.getElementById(checkbox);

if (element.checked)

{

donecol = true;

}

}

 

The code will reach the start of the for loop, and then instead of running the code in the for loop it will go to the code after the for loop.

 

The code will run if I replace the varibale pcount with the value 6 in the for loop statement. So I am looking with the problem somewhere withing the pcount variable. I hope someone can point me in the right direction as to where the error may lie.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.