Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate bootstrap DatePicker through icon

How can I activate my bootstrap DatePicker through clicking an icon?

My HTML code is:

<input type="text" class="datepicker">  
    <span class="add-on"><i class="icon-calendar" id="cal2"></i></span>

and the script is:

<script>
$(function(){
    $('.datepicker').datepicker({
        format: 'mm-dd-yyyy',
        endDate: '+0d',
        autoclose: true
    });
});
</script>

I want to activate my datepicker by clicking on the icon but it is not working.

How can I achieve this?

like image 955
user2142786 Avatar asked Jan 08 '14 07:01

user2142786


People also ask

How do I add a calendar icon in bootstrap?

You can use this icon on the same way in your project. First make sure you have added Bootstrap Icon library. If this library is added just add the HTML css class calendar to any element to add the icon. Bootstrap calendar Icon can be resized as per your need.

How do I add a calendar icon to the input field in bootstrap 4?

Open our free Bootstrap Form Builder in your browser. Add a field from the "Add a Field" tab. Select "Icon" from the Prepend or Append dropdown in the "Edit Fields" tab. Choose an icon from the icon picker window.

Why bootstrap datepicker is not working?

Because you're defining the code in the head the body and its contents haven't been loaded yet; so the selector finds no elements to initialize datepicker. If you don't want to use document. ready functionality you could also move the script to the end of the body tag.


4 Answers

<input type="text" id="my-datepicker" class="datepicker">  
<span class="add-on"><i class="icon-calendar" id="cal2"></i></span>

<script type="text/javascript">"
   $('#cal2').click(function(){
       $(document).ready(function(){
           $("#my-datepicker").datepicker().focus();
       });
   });
</script>
like image 163
makallio85 Avatar answered Oct 31 '22 22:10

makallio85


You can get it done like this, but there is a small change in your html.

<input type="text" class="datepicker" id="tb_date">  
<label class="add-on" for="tb_date">
  <i class="icon-calendar" id="cal2"></i>
</label>
like image 41
Akbar Badhusha Avatar answered Oct 31 '22 22:10

Akbar Badhusha


From docs, Wrap your datepicker textbox with div like

<div class="input-append date" id="dp3">
    <input type="text" id="datepicker" /> 
       <span class="add-on"><i class="icon-calendar" id="cal2"></i></span>
</div>

Then attached the datepicker event with div's id like

$("#dp3").datepicker();

JSFiddle

like image 30
Praveen Avatar answered Oct 31 '22 21:10

Praveen


According to the documentation:

.datepicker('show') Show the datepicker.

Just use the click event to show your datepicker.

like image 29
Loenix Avatar answered Oct 31 '22 21:10

Loenix