vendredi 8 février 2019

jQuery consolidate excessive if/else condition

Checking if there's a more efficient way to deal with this - jQuery or JS. Button ID's are matched up with three types of documents that get their content inserted into textareas in a modal. ID's with a single name work fine, it's names with multiple words that need to be displayed correctly. The style is irrelevant - it's the modal title that's key. The inserts are just external documents with the same name as the button ID (ie. and external docs are primarybutton.html, primarybutton.js, primarybutton.css)

       
       $(".codeBtn").click(function() {
            var btnId = $(this).attr("id");
            var htmlUrl = "./modals/" + btnId + ".html";
            var jsUrl = "./modals/" + btnId + ".js";
            var cssUrl = "./modals/" + btnId + ".css";
            var id = this.id;
            if(id == "primarybutton") {
                $("#modalTitle").html("Primary Button Code");
            } else if(id == "secondarybutton"){
                $("#modalTitle").html("Secondary Button Code");
            } else if(id == "tertiarybutton"){
                $("#modalTitle").html("Tertiary Button Code");
            } else if(id == "segmentedcontrols"){
                $("#modalTitle").html("Segmented Controls Code");
            } else if(id == "time-picker"){
                $("#modalTitle").html("Time Picker Code");
            } else if(id == "textfield"){
                $("#modalTitle").html("Text Field Code");
            } else if(id == "radiobuttons"){
                $("#modalTitle").html("Radio Button Code");
            } else if(id == "textlink"){
                $("#modalTitle").html("Text Link Code");
            } else if(id == "tabbtn"){
                $("#modalTitle").html("Tabs Code");
            }
             else {
            $("#modalTitle").html(btnId + " Code");
            }
             
            $.ajax({
                   url : htmlUrl,
                   dataType: "text",
                   success : function (data) {
                       $(".htmlCode").text(data);
                   }
               });
             $.ajax({
                   url : jsUrl,
                   dataType: "text",
                   success : function (data) {
                       $(".jsCode").text(data);
                   }
               });
             $.ajax({
                   url : cssUrl,
                   dataType: "text",
                   success : function (data) {
                       $(".cssCode").text(data);
                   }
               });
           });
<button data-target="#codeModal" data-toggle="modal" class="codeBtn" id="primarybutton">Get Code</button>


<div class="modal fade text-center" id="codeModal">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modalheader">
                            <h1 id="modalTitle">Navigation Code</h1>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="modal-content-code">
                                <label>HTML:</label>
                                <textarea class="js-copytextarea htmlCode" readonly="readonly" rows="5">Hello. This is textarea test bed #1</textarea>
                                <button class="js-textareacopybtn btn-primary">Copy HTML</button>
                            </div>
                            <div class="modal-content-code">
                                <label>CSS:</label>
                                <textarea class="js-copytextarea cssCode" readonly="readonly" rows="5">Hi! Welcome to textarea test bed #2 </textarea>
                                <button class="js-textareacopybtn btn-primary">Copy CSS</button>
                            </div>
                            <div class="modal-content-code">
                                <label>JS:</label>
                                <textarea class="js-copytextarea jsCode" readonly="readonly" rows="5">Hi! Welcome to textarea test bed #2 </textarea>
                                <button class="js-textareacopybtn btn-primary">Copy JS</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

Aucun commentaire:

Enregistrer un commentaire