user2031327
user2031327

Reputation:

How to include switch statement in javascript for string

In my MVC application have included a button called form Field. whenever user clicks on that button dropdownlist gets displayed in modal box that contains text, checkbox etc as option. code for form field and drop downlist:

<input type="button" id="FormField" name="Form Field" value="Form Field" style="width: 110px; height: 30px; background-color: #FFFFFF;"  onclick="return FormField_onclick()" />


function FormField_onclick(box) {
             dhtmlx.modalbox({
                 title: "Form Field Creation Tool",
                 text: "<div id='form_in_box'><div ><label>Type: <select id='Type' name='Type'><option>Text</option><option>Checkbox</option><option>Radio</option><option>DropDown</option><option>Listbox</option></select></label><br></div><div><span class='dhtmlx_button'><input type='submit' value='Select' style='width: 86px' onclick='Select_type(this)'></span><span class='dhtmlx_button'><input type='button' value='Cancel' onclick='close_file(this)' style='width:80px;'></span></label></div></div>",
                 width: "300px"
             });  
         }

Whenever user selects particular option from dropdownlist for example if user selects text option and clicks Select button than textbox should get inserted at cursor position. code for select button:

 function Select_type(box) {
             var tp = document.getElementById('Type');

             switch (tp) {
                 case "text":
                     {
                     var editor = CKEDITOR.instances.message;
                        editor.insertHtml('<input type="text" id="tx" name="tx" style="width: 110px; height: 30px" />'); 
                 }
                     break;
                 case "Checkbox": { var editor = CKEDITOR.instances.message;
                                        editor.insertHtml('<input type="checkbox" id="chk" name="chk" value="Checkbox" style="width: 110px; height: 30px" />');} 
                     break;
                 case "Radio": 
                     { 
                 var editor = CKEDITOR.instances.message;
                 editor.insertHtml('<input "radio" id="rd" name="rd" value="radio" style="width: 110px; height: 30px" />');
}
                     break;
                 case "DropDown": alert("DropDown");
                     break;
                 case "Listbox": alert("Listbox");
                     break;
             }              
             dhtmlx.modalbox.hide(box);
         }

but this doesn't work for me. Even the alert doesn't work. And also don't know how can i include dropdown and list in it

Upvotes: 0

Views: 289

Answers (2)

FabianCook
FabianCook

Reputation: 20587

You could use an object literal for the switch statement if you wanted to...

var sw = {
    hello: function () {
        console.log("hello");
    },
    goodbye: function () {
        console.log("goodbye");
    }
}
var str = "hello";
sw[str](); //logs hello

Heres an explanation: http://www.dyn-web.com/tutorials/obj_lit.php

I think you need to clarify your question though and tag it appropriately...

Upvotes: 0

Nadav Ben-Gal
Nadav Ben-Gal

Reputation: 549

You want to do the switch on:

document.getElementById('Type').value

and not on the element it self, as it doesn't equals none of the cases you provided.

Upvotes: 1

Related Questions