0

Hi everyone I have a custom range to pick a day in angularjs. I need a listener when i pick two days (start and end) to get the values and autoamtically send this data to my rest api.

View :

<div class="form-group col-md-3 mb0" ng-if="timerange == 'custom'" id="customDiv">
                        <div class="row">
                            <label class="col-sm-3 control-label pt7">Custom</label>
                            <div class="col-sm-9">
                                <input type="text" id="customrange" name="customrange" ng-model="parent.customrange" ng-change="applyOptions()" class="form-control" ui-jq="daterangepicker" ui-options="{format: 'YYYY/MM/DD',startDate: '{{startdate}}',endDate: '{{enddate}}', maxDate:'{{enddate}}'}" />                                
                            </div>
                        </div>
                    </div>

When i click in my range dropdownbox i choose custom and then it popups a input with a date picker.. I need when i press apply to get the data.. I read a lot of documentation and cant add a listener to this... I really need to find a way...

Controller:

var rangeSelector = document.getElementsByTagName('select')[1]; //1h 24h 1w custom                                      
                            if(rangeSelector.value == 'custom'){
                                getDate = controllerScope.getStartEndTimestamps();
                                var datePicker = document.getElementById("customrange").getAttribute('ui-options');

                                var split = datePicker.split(",");

                                var startDate = "";
                                var endDate = "";

                                document.getElemenstByTagName("button")[0].addEventListener("click", function(){ 
                                    alert("I've changed : ");
                                    startDate = split[1].split(': ')[1].replace(/["']/g, '');
                                    endDate = split[2].split(': ')[1].replace(/["']/g, '');
                                });

                                console.log("range selector ",rangeSelector);
                                console.log("startDate ",startDate);
                                console.log("endDate ",endDate);

                                if (startDate !== undefined) {
                                    url += '&startDate='+startDate;
                                }
                                if (endDate !== undefined) {
                                    url += '&endDate='+endDate;
                                }

                            }

Logs:

range selector ​…​​ startDate
endDate

The problem is the alert "I've changed" dont popup... is there a simple way to do this??

enter image description here

8
  • I suspect that document.getElemenstByTagName("button")[0] is undefined, e.g. not rendered yet, try to delegate event to that button, but better to wrap your picker in directive and use pickers api
    – AlexanderT
    Commented Aug 2, 2017 at 11:30
  • Dont understand your comment sorry... I'm new at this.. Commented Aug 2, 2017 at 11:33
  • console.log (document.getElemenstByTagName("button")[0] ) what it gives? Does it undefined or button has been found? if button doesn't rendered, not in DOM at the time you bind handler, it will not work. So you need check if it is there before binding, or you can try [delegate] (api.jquery.com/delegate) even though it deprecated, for testing you could use it and see if alert called.
    – AlexanderT
    Commented Aug 2, 2017 at 11:39
  • document.getElementsByTagName('button')[0] <button class=​"applyBtn btn btn-small btn-sm btn-success" type=​"button">​Apply​</button>​ Is rendered... see my updated question with logs Commented Aug 2, 2017 at 11:43
  • So if you remove condition if(rangeSelector.value == 'custom') alert is shown?
    – AlexanderT
    Commented Aug 2, 2017 at 11:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.