0

I got a problem when I try to create an array of checked checkboxes.

The checkboxes are generated dynamically with an "onChange" attribute that calls the javascript function to add or remove in the array. The function gets "talla" that it is the value to add or remove.

This is my javascript code for the function: (arrayTallas is global)

function checkbox_marcado(talla) 
    {
        if(jQuery('#id_talla').is(':checked') == true)
        {
            arrayTallas.push(talla);

        } 
        else //elimina posicion del array al deseleccionar un checkbox
        {

                var index = arrayTallas.indexOf(talla);         
                arrayTallas.splice(index,1);
        }
    }

The problem is that the first checkbox work fine, but the others are not deleted.

For example. If a have 3 checkboxes with values "1" "2" "3" if I click on the first one, it is added normally, and if I click again on it, it is deleted normally too... but if I click on the first one, and then on the second one, when I click again on the second one to delete it from the array, when I print the array this is what I get: 1 2 2

Thanks

1
  • Thanks that was the main problem, everything works fine now Commented Apr 18, 2011 at 0:01

2 Answers 2

0

If you're using jQuery anyway, why not use a function like this:

$('#tallas').live('click', function(){
   $(':checkbox:checked').map(function() {
     return this.value;
   }).get().join(',');
});
0

I think you mean to say:

jQuery('#id_' + talla)

where you're saying:

jQuery('#id_talla')

Also check to make sure you don't have multiple checkboxes with the same id attribute.

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.