2

Is there any way to bind an event to option tag in jquery or angularjs. I am specifically looking for a click event to a specific option.

Consider an example here

<select>
    <option>Last Month</option>
    <option>Last Year</option>
    <option onclick='showDatePicker()'>Custom Range</option>
</select>

I have a select dropdown in which there are three options, first two are simple whereas the thirdone is little complicated. Whenever a user chooses the Custom Range (option 3) then I need to show a bootstrap model in which I place a bootstrap custom datepicker. I can make it work by adding a change event handler to select element but then the issue arises that what if the user selects the Ccustom Range option and choose the date using datepicker and close the modal. And after that the user tries to select Custom Range option again to let say change the selected date range. Then this change event won't trigger and thus the bootstrap modal won't showup.

Important Note: We are showing the datepicker (bootstrap or any other) in a modal and once user selects the date that modal will be closed.

2
  • 1
    How about ng-click or ng-change? Commented Apr 12, 2016 at 9:25
  • No, it's not possible to bind click event on option element. Use onchange event on select tag or custom selectbox. Commented Apr 12, 2016 at 9:28

4 Answers 4

4

You need to use ngChange

Template:

<select ng-change="showDatePicker()" ng-model="value">
  <option value="month">Last Month</option>
  <option value="year">Last Year</option>
  <option value="custom">Custom Range</option>
</select>

Controller:

function MyCtrl($scope) {
  $scope.isClicked = false;
  $scope.value = 'month';

  $scope.showDatePicker = function() {
    if($scope.value == 'custom') {
      $scope.isClicked = true;
    } else {
      $scope.isClicked = false;
    }
  }
}

Here is a working fiddle: http://jsfiddle.net/trollr/ugfbo9x2/

Sign up to request clarification or add additional context in comments.

Comments

0

it is possible to implement it via ngChange

<select ng-model="select_value" ng-change="changeSelect()">
    <option value="">Last Month</option>
    <option value="">Last Year</option>
    <option value="showDatePicker">Custom Range</option>
</select>

then handle it via the select_value

$scope.changeSelect = function() {
    if ($scope.select_value === 'showDatePicker') {
        // Execute datepicker here
    }
}

Comments

0

The onChange event isn't being triggered because the value didn't change. A simple workaround would be for the value of the dropdown to be cleared if the modal is closed.

So in the handler for your datePicker's select function, you could have like $("datePicker").clear() or something like that

Comments

0

You cannot put any angular code in the <option> part.

However for your scenario of date change on third option selection see the following:

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl"> 
    <select ng-model="selectBox" ng-change="checkIt()">
        <option>One </option>
        <option>Two </option>
        <option>Three</option>
    </select>
    <uib-datepicker ng-model="dt" class="well well-sm" datepicker-options="inlineOptions"></uib-datepicker>
    {{selectBox}} 
    <div ng-show="selectBox=='Three'">
    From: <input type="text" placeholder="fromDate" /> 
    To: <input type="text" placeholder="toDate" />
    </div>
    <script>
    //module declaration
    var app = angular.module('myApp', []);
    //controller declaration
    app.controller('myCtrl',function($scope){
        $scope.checkIt = function(){
            //write the code here for third selection
        }
    });
    </script> 
</body> 
</html> 

Use 'datepicker' of Angular Bootstrap for using datepickers:

https://angular-ui.github.io/bootstrap/#/datepicker

3 Comments

But again, this is exactly what I have mentioned that what if the user selects the 3rd option and do some date selection and close the popup and after that try to select the 3rd option again which is already selected and thus no change event fires up than how can we handle the same.
Bro, as long as third option is selected your from and to date will be visible. So why will he need to select third option again. He can change the values directly on visible from and to date. wont he ?
No. Because, we are going to show the datepicker in a popup and as soon as the user selects the date he can close the popup(modal).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.