Samuurai
Samuurai

Reputation: 2645

Javascript/JQuery if statement

This is probably pretty simple, but I can't figure out how to do it: I have this code:

$.post("/admin/contract", {
                          'mark_paid' : true,
                          'id' : id
        },

In pseudo code, how can I do this:

$.post("/admin/contract", {
                          'mark_paid' : true,
                          'id' : id,
                          if(is_set(dont_email)) {print 'dont_email' : true}
        },

Upvotes: 1

Views: 142

Answers (5)

ghoppe
ghoppe

Reputation: 21784

I'm not sure I understand your question correctly. Is dont_email a variable? Are you checking the existence of dont_email?

var myDontEmail = (typeof(dont_email) != "undefined");

$.post("/admin/contract", {
             'mark_paid' : true,
             'id' : id,
             'dont_email': myDontEmail
        },

Upvotes: 0

antyrat
antyrat

Reputation: 27765

Try this:

$.post("/admin/contract", {
                 'mark_paid' : true,
                 'id' : id,
                 'dont_email': (is_set(dont_email))?true:false
            },

Upvotes: 0

Peter Bailey
Peter Bailey

Reputation: 105888

How I'd do it.

$.post("/admin/contract", {
  'mark_paid' : true,
  'id' : id,
  'dont_email' : ( 'undefined' != typeof dont_email )
},

Upvotes: 7

Ross
Ross

Reputation: 14415

var details = {
    'mark_paid' : true,
    'id' : id,
}

if(is_set(dont_email)) {
    details.dont_email = true;
}

$.post("/admin/contract", details);

untested...

Upvotes: 1

Tomas Vana
Tomas Vana

Reputation: 18775

I'll give it a try :

$.post("/admin/contract", {
                          'mark_paid' : true,
                          'id' : id,
                          'dont_email' : is_set(dont_email) ? true : undefined
        },

Upvotes: 2

Related Questions