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?
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.
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.
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.
<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>
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>
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
According to the documentation:
.datepicker('show') Show the datepicker.
Just use the click event to show your datepicker.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With