0

I am now practicing my JS skills and Jquery.

var clickcheckboxcount=0; 
var clickedcheckboxname = new Array();
var clickedcheckboxvalue = new Array();
$("#input[type=checkbox]").click(function(){
    if ($('div:not([class~=clicked])'){
    $(this).addClass(clickcheckboxcount+'clicked');
    clickedcheckboxname.push($(this).attr("name"));
    clickedcheckboxvalue.push($(this).val());
    clickedcheckboxcount+=1;
}

there is serval checkbox with different name and value in HTML body part. I just want to get their attr and name by click them and don't want push same value in the array. So How can I rewrite if() function.

I put my codes demo in http://jsfiddle.net/Lza2L/

WillDo
  • 123
  • 1
  • 9

2 Answers2

1

You can try to use :not selector:

$('div:not(.0clicked,.1clicked,.2clicked,.3clicked,.5clicked,.5clicked)')

or is():

if (!$(this).is(".0clicked, 1clicked, 2clicked, 3clicked, 4clicked, 5clicked"))) {  
    // Your code here
}

1) You need to include jQuery in your Fiddle

2) Change #input[type=checkbox] to input[type=checkbox]. # used to target id. You don't need it here in your selector.

Updated Fiddle

Felix
  • 37,892
  • 8
  • 43
  • 55
0

I would think an attribute selector combined with not() would work in this case:

$('div:not([class$=clicked]);

This selects elements having a class value that ends with 'clicked'. Of course, complications arise in cases where the element has more than one class.

As Arun points out below, the Contains Word selector may be better suited:

$('div:not([class~=clicked]);
isherwood
  • 58,414
  • 16
  • 114
  • 157