Toggle a select and a datapicker

1

I'm trying to implement a field that can be a select or a datepicker .

But inserting the code into <div> causes the datepicker to stop working. I leave the code below for anyone who can help me.

<!doctype html>

<html>
<head>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
    <script src="https://code.jquery.com/jquery-1.8.2.js"></script><scriptsrc="https://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
    <meta charset="utf-8" />
    <script>
    $(function() {
    $( "#calendario" ).datepicker();
    });
    </script>
    <script>
    function desligar(){
       var divData = document.getElementById('data');
       divData.innerHTML="<p>Data: <select id='calendario'><option value='01/03/2018'>01/03/2018</option><option value='01/04/2018'>01/04/2018</option></select></p>";
    }
    function ligar(){
       var divData = document.getElementById('data');
       divData.innerHTML="<p>Data: <input type='text' id='calendario' /></p>";
    }
    </script>
    <title>Calendário jQuery</title>
</head>
<body>
    <div id='data'>
    <p>Data: <input type="text" id="calendario" /></p>
    </div>
<BR>
<BR>
<input type='button' onClick=ligar(); value='calendario'>&nbsp;&nbsp;
<input type='button' onClick=desligar(); value='select'>
 </body>
</html>
    
asked by anonymous 05.03.2018 / 20:59

1 answer

0

Just put $( "#calendario" ).datepicker(); in function ligar()

$(function() {
    $( "#calendario" ).datepicker();
});

    function desligar(){
       var divData = document.getElementById('data');
       divData.innerHTML="<p>Data: <select id='calendario'><option value='01/03/2018'>01/03/2018</option><option value='01/04/2018'>01/04/2018</option></select></p>";
    }
    function ligar(){
       var divData = document.getElementById('data');
       divData.innerHTML="<p>Data: <input type='text' id='calendario' /></p>";
        $( "#calendario" ).datepicker();
    }
<!doctype html>

<html>
<head>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
    <script src="https://code.jquery.com/jquery-1.8.2.js"></script><scriptsrc="https://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
    <meta charset="utf-8" />

    <title>Calendário jQuery</title>
</head>
<body>
    <div id='data'>
    <p>Data: <input type="text" id="calendario" /></p>
    </div>
<BR>
<BR>
<input type='button' onClick=ligar(); value='calendario'>&nbsp;&nbsp;
<input type='button' onClick=desligar(); value='select'>
 </body>
</html>
    
06.03.2018 / 02:32