Toggle a select and a datapicker

Asked

Viewed 68 times

0

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

But when inserting the code into the <div> causes the operation of the datepicker. I keep the code down 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>
    <script src="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>

  • Guy with a simple "hidden" checkbox and CSS vc can switch between a input text and a input date. If you want me to put an answer with an example for you to see if it suits you.

  • 1

    I already did.. I loved the idea.. Sometimes thinking outside the box, it’s the best solution.. Thank you very much...

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>
    <script src="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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.