/    Sign up×
Community /Pin to ProfileBookmark

Validating an array of dynamically created checkboxes

Hi all, I have an array of dynamically created checkboxes name Delete[]. Each one has a different number value based on an ID field from a database.

What I need to do is detect if any of these were checked and if not then raise an Alert message.

Thanks!?

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JAug 16.2003 — Can you post your code for us to work on
Copy linkTweet thisAlerts:
@Khalid_AliAug 16.2003 — Take a look at this script,a bit advance,however it explains completely how to use checkbox validation

http://www.webapplikations.com/pages/html_js/forms/CheckBoxesLimitUserSelection.html
Copy linkTweet thisAlerts:
@ArcauthorAug 16.2003 — I had a look at that code but I didnt really see anything that applied to what I need. Thanks anyway.

Here is the code I am working on, it's pretty basic stuff, just loop thru the forms elements and see if it is a check box and if it is see if it's checked. If none are checked then raise an alert.

[code=php]
function Validate() {
var form = document.forms[0]
var counter=0;
for (i = 0; i < form.elements.length ; i++) {
if (form.elements[i].type == "checkbox"){
If(form.elements[i].checked == "TRUE"){
counter++
}
}
}
if (counter == 0) alert("You have not selected an item to delete");
}
[/code]



The problem I am having trouble with it is around the If(form.elements[i].checked == "TRUE") part. I am not sure if that's how to check if a checkbox is checked or not.



Thanks!
Copy linkTweet thisAlerts:
@Khalid_AliAug 16.2003 — [i]Originally posted by Arc [/i]

[B]I had a look at that code but I didnt really see anything that applied to what I need. [/B][/QUOTE]


The link uses the validation that checks for which checkboxes are checked and which are not..I guess its beyond your comprehension...and you want some one make to make the exactly code that you have work for you...

I am sure some one will have time on hand..
Copy linkTweet thisAlerts:
@Mr_JAug 16.2003 — I guess its beyond your comprehension..[/QUOTE]

Not really the right attitude is it [B]Khalid Ali[/B]



[B]Arc[/B]


The only error I found in your script was the capital TRUE in quotes.

See example below

<script>

function Validate() {

var form = document.forms[0]

var counter=0;

for (i = 0; i < form.elements.length ; i++) {

if (form.elements[i].type == "checkbox"){

if(form.elements[i].checked == true){

counter++

}

}

}

if (counter == 0){

alert("You have not selected an item to delete");

}

else{

alert("You have selected "+counter+" checkboxes")

}

}



</script>





<form>

<input type="checkbox"><br>

<input type="checkbox"><br>

<input type="checkbox"><br>

<input type="checkbox"><br>

<input type="checkbox"><br>

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

</form>







Copy linkTweet thisAlerts:
@ArcauthorAug 16.2003 — [i]Originally posted by Khalid Ali [/i]

[B]The link uses the validation that checks for which checkboxes are checked and which are not..I guess its beyond your comprehension...and you want some one make to make the exactly code that you have work for you...



I am sure some one will have time on hand.. [/B]
[/QUOTE]



No it's Not "Beyond my comprehention". There is a big difference between what I need and what that offers.

I need code that works with an array of check boxes that are dynamically created. There could be 5 there could 500.

The code you showed me works with a hard coded number of checkboxes that are not in an array. So as I said it doesn't apply to what I am doing.

And BTW what's with the major attitude?
Copy linkTweet thisAlerts:
@ArcauthorAug 16.2003 — Cool, thanks Mr j i'll give that a shot.?
Copy linkTweet thisAlerts:
@Khalid_AliAug 16.2003 — [i]Originally posted by Mr J [/i]

[B]Not really the right attitude is it [/B][/QUOTE]


Its the right attitude.

This forum is for help to those who are stuck on some javascript issues,but for those who need free consulting,its nto the place.

The purpose of mine and others like me to post links is to provide the resources for those who need to learn the language.

Of course someone who thinks its a place to get free consultation its not the right attitude,,and for you I can only say mind your own biz...if you have something to add just do it dont judge others.
Copy linkTweet thisAlerts:
@ArcauthorAug 16.2003 — Well do me and others a favor then and don't post non relevant links and then proceed to call the poster a moron because they didn't find the link usefull.

Firstly, I didn't come here and ask for anything all that complex, just a simple loop thru an array of checkboxes.

Secondly, if this board isn't for free consultation then what is it for? To post links from other sites?

Lastly, How did someone with such a piss poor attitude get to be a moderator? Were they getting too many visitors and decided to get someone to run some off? Cause it's working.


And BTW Mr J that worked great, thanks again!?
Copy linkTweet thisAlerts:
@Khalid_AliAug 16.2003 — Our attitude here is for people like yourself. who think they can come here and get complete solutions.

You should have the decency to look and see if the code I recomended gave you a simple answer how to validate that a check box is selected or not and that was your question too.

But you wanted to have a complete solution written for you..Thank god this site does not depend on people who look for free stuff,its a place where people come in most cases to learn.

And yes it wasn't that complex.Look at these lines these are from the resource I posted

var frm = document.getElementById("form1");

for(x=0;x<frm.length;x++){

if(nctr<allowedNumberOfSelections && frm[x].type=="checkbox" && frm[x].checked){

nctr++;

}else if(nctr>=allowedNumberOfSelections && frm[x].checked){

frm[x].checked = false;

return false;

}

And this is how you responded.

[b]

I had a look at that code but I didnt really see anything that applied to what I need[/b]



And here is the solution that you think is good for you posted by another member above.

for (i = 0; i < form.elements.length ; i++) {

if (form.elements[i].type == "checkbox"){

if(form.elements[i].checked == true){

counter++

}



Can you see if thats not simmilar to the one above.



now if you look at the code or anyone who knows what it means,and refer to your primary question that you wanted to validate if a checkbox is cehcked, will know that its exactly what you asked.

My point of all this exercise is that if you have no idea what this code is about don't say it doesn't apply to u(only because you don't understand it),you could have just asked that you needed more description.



We are volunteers here who like to help,but we are not here to take crap from those who don't understand.
Copy linkTweet thisAlerts:
@ArcauthorAug 17.2003 — Actually that code is pretty similar. BUt i'll be damned if i could find it on that page, I even tried view source. Then I realized it must be in one of the included js files in a different directory, so I coppied the path and attached it to the directory you gave minus the last directory and file and I got the js page. Then i was able to view the code you just showed.


However I wasnt aware all of that was needed to begin with... i just viewed he page itself and didnt see anything resembling what I needed.
Copy linkTweet thisAlerts:
@Khalid_AliAug 17.2003 — [i]Originally posted by Arc [/i]

[B]However I wasnt aware all of that was needed to begin with...[/B][/QUOTE]


Well applogy accepted...
Copy linkTweet thisAlerts:
@Mr_JAug 17.2003 — Khalid

Our attitude here is for people like yourself. who think they can come here and get complete solutions. [/QUOTE]


Is this the attitude of all the moderators of this forum or just yours.

What about people who are just learning javascript, are they to be banned from this forum then

Had you given a more precise answer to the question this debate would not in motion.

Maybe the members of this forum should be informed that [B]complete[/B] solutions will not be given on the basis that this [B]free stuff[/B] is not the policy of this forum.

I had a similar response to one of my own threads where this guy only told me what I already new but he was not prepared to delve any deeper into the problem because he said

"I get paid for that"

Apparently he was a web consultant of sorts.

But my point is that my thread was wasted because if people see that the thread has been answered a few times then they probably won't look.

Well I think enough has been said on matter.

unsubscribe=true
Copy linkTweet thisAlerts:
@ArcauthorAug 17.2003 — [i]Originally posted by Khalid Ali [/i]

[B]Well applogy accepted... [/B][/QUOTE]



Haha, whatever dude. That wasn't an appology. I don't beleive I owe you an apology.
Copy linkTweet thisAlerts:
@Khalid_AliAug 17.2003 — I guess if you are not ashamed of your behaviour and the wording of your earlier posts above, and having accepted that it was you who did not understand the response,,,well I can only say that you are an ignorant person who will learn the hard way..and good luck..because you will need it believe me.
Copy linkTweet thisAlerts:
@ArcauthorAug 17.2003 — Well, I didn't come to these boards to get into a big argument, I just wanted a little help with a script.

But your sad and rude attitude is what started the entire thing, so maybe you need to look in the mirror?

Yes I admit I didn't understand your response, but in all reality the link you provided was not an answer to my question. I had to go thru the source on that page to find a partial link to another page and then merge the two links together to get the page that actually had the script on it..Cofusing? to say the least. And I kindly said I didnt find that link helpfull but thanks anyway, then you come in with your bad attitude and start implying that i'm a complete moron instead of being helpfull and explaining where the code actually was.

Anypoo, I won't be responding to this thread any more.
×

Success!

Help @Arc 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.3,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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