-1

I have this function for building HTML.

CreateInputCheckboxActive: function (id, mainClass, checked, headerTxt, inputTxt) {
    var bootstrapClass = "col-sm-6 ";
    return (
     $("<div/>", { class: bootstrapClass + mainClass }).append(
         $("<label/>", { "text": headerTxt }),
         $("<input/>", {
            "id": mainClass,
            "class": mainClass,
            "type": "checkbox",
            "checked": checked
         }),
         $("<label/>", {
            "class": mainClass,
            "for": mainClass,
            "text": checked == true ? inputTxt[0] : inputTxt[1]
         })
         )
     );
}

How can I create the first label just if the parameter headerTxt has a value? I want to use this way of jquery.

  • `if (headerText) {}` – Liam Jan 23 '18 at 09:47
  • 1
    Possible duplicate of [Is there a standard function to check for null, undefined, or blank variables in JavaScript?](https://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in) – Liam Jan 23 '18 at 09:48
  • Thanks. I can not add if inside the function append. it gives an error!! –  Jan 23 '18 at 09:49
  • I'm pretty sure you can add an if into that function... – Liam Jan 23 '18 at 09:53

2 Answers2

3

Try to add a condition before appending the <label> like :

CreateInputCheckboxActive: function (id, mainClass, checked, headerTxt, inputTxt) {
        var bootstrapClass = "col-sm-6 ";
        var parent = $("<div/>", { class: bootstrapClass + mainClass });
        if(headerTxt)
           parent.append($("<label/>", { "text": headerTxt }));
        parent.append($("<input/>", {
                "id": mainClass,
                "class": mainClass,
                "type": "checkbox",
                "checked": checked
             }),
             $("<label/>", {
                "class": mainClass,
                "for": mainClass,
                "text": checked == true ? inputTxt[0] : inputTxt[1]
             })
        );
        return parent;
    }
GGO
  • 2,678
  • 4
  • 20
  • 42
0

Wrap the first label in a ternary if statement:

(headerTxt ? $("<label/>", { "text": headerTxt }) : ''),

Full Code:

CreateInputCheckboxActive = function (id, mainClass, checked, headerTxt, inputTxt) {
        var bootstrapClass = "col-sm-6 ";
        return (
         $("<div/>", { class: bootstrapClass + mainClass }).append(
             (headerTxt ? $("<label/>", { "text": headerTxt }) : ''),
             $("<input/>", {
                "id": mainClass,
                "class": mainClass,
                "type": "checkbox",
                "checked": checked
             }),
             $("<label/>", {
                "class": mainClass,
                "for": mainClass,
                "text": checked == true ? inputTxt[0] : inputTxt[1]
             })
             )
         );
    };
    
    jQuery('body').append(
    
      //With Header
      CreateInputCheckboxActive('not-used', 'class-used-for-id-and-class', false, 'Header Text', ['Checked', 'Not Checked']),
      
      '<br/>',
      
      //Without Header
      CreateInputCheckboxActive('not-used', 'class-used-for-id-and-class', true, null, ['Checked', 'Not Checked'])
      
    );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129