tags:

views:

45

answers:

1

Hi, I need some help shortening this code and am just a little off. Here is original code:

$("#MKDPT").change(function() {
 if ($("#MKDPT").is(":checked") && $("#AMX1N").is(":not(:checked)") ||
 ("#ERXXN").is(":not(:checked)")) {
         alert(this.id);
     }    
 });

I will be adding a lot more id's. Can I just create an array of the ID's im making sure aren't checked and compare against it? Im not sure if you can check to see if any item in an array is checked, or do I need to do it by .length?

Example

 $("#MKDPT").change(function() {
       if ($("#MKDPT").is(":checked") && $("#AMX1N").is(":not(:checked)") ||
 ("#ERXXN").is(":not(:checked)") || ("#ID").is(":not(:checked)") || ("#ID").is(":not(:checked)") ||
 ("#ID").is(":not(:checked)") || ("#ID").is(":not(:checked)") || ("#ID").is(":not(:checked)")) {
         alert('foo');
     }    
 });

this is what I want it to do, but am having trouble with:

 $("#MKDPT").change(function() {
       var ckd = new Array = ("AMX1N","ERXXN","ID","ID","ID","ID","ID","ID","ID")
       if ($("#MKDPT").is(":checked") && ckd.is(":not(:checked)")) {
         alert('foo');
     }    
 });

Kane Leins

A: 

One way is to go about like this:

$("#MKDPT").change(function() {
       var ckd = new Array ("AMX1N","ERXXN","ID");

       for(var i = 0; i < ckd.length; i++)
       {
          if ($("#" + ckd[i]).is(":checked")) {
             alert('foo');
             break;
          }
       }
 });

The for loop goes through each of the array element and checks to see if any element with such id is checked and if yes, it alerts about that.

Sarfraz
Can you pls explain the "for(var p in ckd)" what is p standing in for? and what does the "break" do?
Dirty Bird Design
@Bird: It is a auxiliary variable created in order to loop through the array, you can name it anything. More info: http://www.w3schools.com/JS/js_loop_for_in.asp
Sarfraz
Don’t use `for … in` loop for arrays; it is to be used to iterate object properties only. Use the normal `for` loop for arrays instead.
Gumbo
@Gumbo: Thanks but it seems to be fine as per this: http://www.w3schools.com/JS/js_loop_for_in.asp. Any particular reason for this? What i suspect is that it is more appropriate for objects.
Sarfraz
So it would be for(var p ??) How would you do it w/o the for..in loop?
Dirty Bird Design
@Bird: See my updated answer please.
Sarfraz
@sAc: W3Schools is no reliable reference. Just think of the situation when the prototype of *Array* is extended with some custom method like this: `Array.prototype.foobar = function() {}`. With `for … in` you will get `foobar` because `foobar` is a property of an object of *Array*.
Gumbo
@sAc: each iteration in a `for..in` in loop has considerably more overhead and therefor is way slower. Only choose that loop if you dont' how many objects to iterate over.
jAndy
@sAc: It needs to be `i < ckd.length`.
Gumbo
@jAndy: It’s primarily not about performance but about correctness.
Gumbo
@Gumbo: correctness in that context is pretty 'subjective' I guess? Performance is a fact.
jAndy
@Gumbo, @jAndy: That was useful, thank you both :)
Sarfraz
Here is what I have, and doesn't seem to work right...$("#MKDPT").change(function() { var ckd = new Array ("AMX1N","ERXXN"); for(var i = 0; i < ckd.length; i++) { if ($("#MKDPT" + chk[i]).is(":checked")) { alert('foo'); break; } } });
Dirty Bird Design
@Bird: Is there any error you are getting? What is that?
Sarfraz
chk is not defined[Break on this error] if ($("#MKDPT" + chk[i]).is(":checked")) {
Dirty Bird Design
@Bird: Make sure that you have included the jquery libaray in your page and wrap your code in ready handler: `$(function(){// your code here});`
Sarfraz
This is in an external .js file. when i wrap it in $(function... in the external file still get same error
Dirty Bird Design
sAc - You altered the variable name in your code. `ckd` is being referenced as `chk[i]`
patrick dw
@patrick: O yes did not notice that. Thanks
Sarfraz
@patrick thanks nice catch
Dirty Bird Design
I ended up using it as a validation rule, not a change event. Do you see any way to tighten this up? Currently it doesn't remove the error if you do select one of the elements in var ckd (sorry this code formatting sucks on comments) see next post
Dirty Bird Design
$.validator.addMethod('MKDPTOnly', function(value, element) { var ckd = ['AMX1N','BMFNP','BMFNB','CFE1N','CBEXR','CBT1','CBTQL','CME1','CMEQL','CMX1','DCMX1','DMERT','DMEDL','ERX1N','ERXXN','EXC1N','EXD1N','EXR1N','EXI1N','IPE1','KCB1','LME1','DLME1','MPL1','NDQ1N','NDQL2','CEC1','ICEFI','NYM1','DNYM1','NYMQL','NYS1N','NYSLM','PNK1P','PNK2N','SMX1','CNC1N','CNS1N','MTL1N','MTL2N','WEA1']; var otherChecked = $('#' + ckd.join(',#')).filter(':checked').length > 0; return !$(element).is(':checked') || otherChecked; },
Dirty Bird Design
'Market Depth ($20) is only required with a Real Time Exchange.');
Dirty Bird Design