0

I am showing a form to the user with few checkboxes. Some of them will be pre-selected and some won't be.

like below:

<input type="checkbox" name="box" value="9"> nine
<input type="checkbox" name="box" value="10"> ten
<input type="checkbox" name="box" value="11"> eleven
<input type="checkbox" name="box" value="12" checked> twelve

Please note that in the last checkbox the checked keyword is added using a scriplet.

Now, even when I physically deselect check twelve, my javascript code doesn't understand that the checkbox is no longer selected.

Update I pasted the wrong link earlier. Here is the updated jsfiddle link: http://jsfiddle.net/JLSUK/20/

What I'm trying to accomplish alert the user ONLY when they have selected checkbox twelve. However, even if that checkbox is not selected, I'm getting an alert that it IS selected. I think this is because I have checked in the actual HTML content, however, I can't remove that since the first time the page loads, I have to show to the user that they had this particular checkbox selected.

Is this doable with jQuery?

3
  • are you really using jQuery 1.2.6, as you selected in the fiddle? Commented Jun 27, 2012 at 14:46
  • in your demo, you wanna see if any one of theses checkbox's are selected ? Commented Jun 27, 2012 at 14:46
  • 1
    What question are you asking? How to prevent the checkbox from being selected? or How to know when it is clicked? Commented Jun 27, 2012 at 14:47

2 Answers 2

1

In your jsfiddle you have:

$('#update').click(function () {

Whereas your input is:

<input type="submit" id="something" value="update" /> 

​Try changing your jquery to:

$('#something').click(function () {

The #* selector in jQuery is referring to the id, not the value.


Also you have:

if (jQuery.inArray('8', selected) >= 0 && jQuery.inArray('11', selected) >=0)

None of your checkboxes have a value of 8 so this will never be "true".


Also declare the array as a new array:

var selected = new Array();

SEE WORKING DEMO

Select "eight" and "eleven" and your popup will appear


EDIT: After seeing question edit, you need to add the following line to clear the previous array items:

var selected = [];

http://jsfiddle.net/JLSUK/20/

Sign up to request clarification or add additional context in comments.

10 Comments

yeah, it does, but it suggests a lack of familiarity with what's considered current best practise...
@Alnitak Do you have any source for this information? I'm not questioning you, but would like some evidence before I change my methods.
what u think of that ? jsfiddle.net/JLSUK/14 made one not using a array, i dont know if is realy this what he needs
It was in one of Crockford's videos - I forget which.
@RicardoArruda I won't dispute whether there is a way of achieving the same results without an array. One could simply check whether a checkbox with a value of 8 and a checkbox with a value of 11 are selected. But your example does not do this.
|
1

You are adding elements to the array, but you are never removing them. So every time the user clicks 'update', you are simply filling your array. At some point you either need to clear the array, or remove the previous selection from it (depending on your exact logic).

1 Comment

This would work! Let me see how I can remove elements from the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.