javascript - Remove timepicker effect from textbox -


i have seen previous question asked problem. don't want replace textbox same id.

when timepicker selected in dropdown timepicker should enable in textbox(which run expected)

when dropdown value changes textbox textbox should treated simple text

    function changetype(control){      	if(control.value == "time"){        	$("#txtinput").timepicker();        }        else {          $("#txtinput").val('');        	// want remove timepicker textbox        }              }
.input-group-addon:after{        content:'\0777'      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-timepicker/0.5.2/css/bootstrap-timepicker.min.css" rel="stylesheet"/>  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-timepicker/0.5.2/js/bootstrap-timepicker.min.js"></script>  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet"/>  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>    <select name="" id="cmblist" onchange="changetype(this)">         <option value="text">textbox</option>         <option value="time">timepicker</option>      </select>      <br><br><br>      <div class="input-group bootstrap-timepicker timepicker">        <input id="txtinput" type="text" class="form-control">        <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>      </div>

live demo

i have seen previous question asked problem. don't want replace textbox same id.

that's among few possible workarounds can use if plugin doesn't come built-in function this. can try removing event handlers , new elements plugin creates if don't want replace original element.

this not perfect solution small hack. i'm doing since timepicker doesn't come built in function de-initialize it, i'm making copy of element, removing actual element ( has plugin initialized ), , replacing new element.

    function changetype(control){      	if(control.value == "time"){          /* getting outer html of input , next element ( button brings timepicker ) */          var elem = $("#txtinput").get(0).outerhtml;          var elem_next = $("#txtinput").next().get(0).outerhtml;            /* adding temporary class identify original input */          $("#txtinput").addclass('remove-this-now');            /* removing button next input */          $('.remove-this-now').next().remove();          $('.remove-this-now').after(elem + elem_next);            /* removing input */          $('.remove-this-now').remove();            /* initializing timepicker new input */        	$("#txtinput").timepicker();        }        else {          $("#txtinput").val('');            /* getting outer html of input , next element ( button brings timepicker ) */          var elem = $("#txtinput").get(0).outerhtml;          var elem_next = $("#txtinput").next().get(0).outerhtml;            /* adding temporary class identify original input */          $("#txtinput").addclass('remove-this-now');            /* removing button next input */          $('.remove-this-now').next().remove();          $('.remove-this-now').after(elem + elem_next);            /* removing input */          $('.remove-this-now').remove();        }              }
.input-group-addon:after{        content:'\0777'      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-timepicker/0.5.2/css/bootstrap-timepicker.min.css" rel="stylesheet"/>  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-timepicker/0.5.2/js/bootstrap-timepicker.min.js"></script>  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet"/>  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>    <select name="" id="cmblist" onchange="changetype(this)">         <option value="text">textbox</option>         <option value="time">timepicker</option>      </select>      <br><br><br>      <div class="input-group bootstrap-timepicker timepicker">        <input id="txtinput" type="text" class="form-control">        <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>      </div>


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -