Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datepicker on click of icon

I want to show the jQuery datepicker on click of a FontAwesome icon which is inside the input field. Below is the code:

<div class="col-md-4 col-sm-4 col-xs-12">
  <label for="datepicker">Date of birth*</label>
  <input type="text" name='datepicker' class="form-control"  value="Select date" id="datepicker" ng-required="true" placeholder="MM/DD/YYYY" >
  <span class="fa fa-calendar"></span>
</div>
jQuery('.fa-calendar').on('click' , function() {
  jQuery(this).datepicker().trigger('focus');
});
like image 520
Mathi Avatar asked Apr 25 '17 08:04

Mathi


People also ask

How to show calendar icon in jQuery DatePicker?

png", buttonImageOnly: true, buttonText: "Select date", changeMonth: true, changeYear: true, yearRange: "-100:+0" }); }); buttonImage in above jQuery script showing image of calendar icon, you can set any other icon that you like, showOn: “both” will show calendar on click icon or text field.

How to show DatePicker using JavaScript?

Syntax: $(". selector"). datepicker( { showOn: "focus"} );


1 Answers

Try this

$(document).ready(function() {
  $("#datepicker").datepicker();
  $('.fa-calendar').click(function() {
    $("#datepicker").focus();
  });
});
  • Initialise datepicker once only

Here is the example

$(document).ready(function() {
  $("#datepicker").datepicker();
  $('.fa-calendar').click(function() {
    $("#datepicker").focus();
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="col-md-4 col-sm-4 col-xs-12">
  <label for="datepicker">Date of birth*</label>
  <input type="text" name='datepicker' class="form-control"  value="Select date" id="datepicker" ng-required="true" placeholder="MM/DD/YYYY" >
  <span class="fa fa-calendar"></span>
</div>
like image 50
Abi Avatar answered Oct 13 '22 12:10

Abi