-2

I would like to Add a if statement to surround some code. I need my if statement to be (but in jquery):

If (chkSurety.checked = true){
SuretyFullName: { required: true }, // This is validation plugin and must stay like this.
SuretyIDNumber: { required: true}, 
SuretyPhysicalAddress: { required: true }, 
SuretyHomeTel: { required: true}, 
SuretyPlace: { required: true}, 
SuretyDay: { required: true }, 
SuretyMonth: { required: true } 
}
Else
{
SuretyFullName: { required: false },
SuretyIDNumber: { required: true}, 
SuretyPhysicalAddress: { required: true }, 
SuretyHomeTel: { required: true}, 
SuretyPlace: { required: true}, 
SuretyDay: { required: true }, 
SuretyMonth: { required: true } 
}

This if statement is C# i need it to be jquery. This is inside a event already so don't want the checkbox.checked to be the event, just need the above code in jquery please.

Thanks in advance, if you need more info just ask :)

2

5 Answers 5

3

Assuming that you checkbox has an ID of chkSurety, and that all you want to do is update the SuretyFullName object's state, per the code given in your question:

SuretyFullName.required = $('#chkSurety').is(':checked');

or (more efficiently) without jQuery:

SuretyFullName.required = document.getElementById('chkSurety').checked;

It appears that you're trying to create some rules for the jQuery validation plugin, hence:

// set the default rules
var rules = {
    SuretyFullName: { required: false },
    SuretyIDNumber: { required: true}, 
    SuretyPhysicalAddress: { required: true }, 
    SuretyHomeTel: { required: true}, 
    SuretyPlace: { required: true}, 
    SuretyDay: { required: true }, 
    SuretyMonth: { required: true } 
 };

 // override the full name rule, based on the checkbox
 rules.SuretyFullName.required = $('#chkSurety').is(':checked');

 $('#fmrMain').validate({
      rules: rules,
      ...
 });
10
  • sorry i need a if statement this wont work for what i need to do, i just tried it as a test and its not right.
    – Pomster
    May 18, 2012 at 7:33
  • @Pomster if the required result is exactly as in your question then the code above would work. If you need additional things to happen depending on the check button state, you should have said so.
    – Alnitak
    May 18, 2012 at 7:35
  • i cant have .required my format needs to be: SuretyFullName: { required: true } and have the if statement souround that
    – Pomster
    May 18, 2012 at 7:38
  • @Pomster then you need to explain what you think SuretyFullName: { required: true } means, because to a JS programmer it means setting the nested property .required of another object that itself has a property called SuretyFullName to true.
    – Alnitak
    May 18, 2012 at 7:42
  • Please look at my edit and u will see that the if statement needs to be one if statement regarding my checkbox only please adjust your answer.
    – Pomster
    May 18, 2012 at 7:43
1

Try this:

if ($("#chkSurety").is(":checked")) {
    // rest of code...
}
1
  • Need the else statement to, and that if is not working, i had a green line underlineing the if
    – Pomster
    May 18, 2012 at 7:40
1

Simply put this:

var yourObject = {SuretyFullName: { required: chkSurety.checked }};

If you really need if-syntax for some reason:

var yourObject = {};
if (chkSurety.checked) {
    yourObject.SuretyFullName = true;
} else {
    yourObject.SuretyFullName = false;
}

Note: if (chkSurety.checked == true) is redundant, if (chkSurety.checked) is better.

1

If you can help it don't include the entire JQuery library if it's just as easy to use pure JavaScript

However as it's a requirement to use JQuery :-)

if ($('#chkSurety').is(":checked")) {
    // checked
} else {
    // not checked
}

​ ​ http://jsfiddle.net/XXHrE/3/ - to test it out...

4
  • My question already looks like that so that's a effortless answer :|, i have requirement's to use jquery.
    – Pomster
    May 18, 2012 at 7:45
  • @Pomster is this more like it?
    – Greg
    May 18, 2012 at 7:55
  • @Greg .attr('checked') is not the right way to check the state of a checkbox.
    – Alnitak
    May 18, 2012 at 7:57
  • @Greg yup, it looks like everyone else's now :)
    – Alnitak
    May 18, 2012 at 8:03
0

This works perfectly.

SuretyFullName: { required: function (element) { if ($('#chkSuretyShip').is(':checked')) { return true } else { return false} } }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.