0

How can i check when a value on input is changed.

Image of calendar control

I have a calendar and when i click on calendar it changes the value on input , but when im trying to see if it has changed its not working. i have tried AddEventListener, also jquery on change, also i sent a function on change to call it but none of them is working.

  <input type="text" id="date" class="date" onchange="changed()" name="" >
   function changed(){
      alert("hello world");
    }

Main js file for creating the calendar : This function creates the calendar on my php file . And then when on click it gets the value on the input with id #date But When im trying to see if value has changed it is not working .

// Initialize the calendar by appending the HTML dates
function init_calendar(date) {
    $(".tbody").empty();
    $(".events-container").empty();
    var calendar_days = $(".tbody");
    var month = date.getMonth();
    var year = date.getFullYear();
    var day_count = days_in_month(month, year);
    var row = $("<tr class='table-row'></tr>");
    var today = date.getDate();
    // Set date to 1 to find the first day of the month
    date.setDate(1);
    var first_day = date.getDay();
    // 35+firstDay is the number of date elements to be added to the dates table
    // 35 is from (7 days in a week) * (up to 5 rows of dates in a month)
    for(var i=0; i<35+first_day; i++) {
        // Since some of the elements will be blank, 
        // need to calculate actual date from index
        var day = i-first_day+1;
        // If it is a sunday, make a new row
        if(i%7===0) {
            calendar_days.append(row);
            row = $("<tr class='table-row'></tr>");
        }
        // if current index isn't a day in this month, make it blank
        if(i < first_day || day > day_count) {
            var curr_date = $("<td class='table-date nil'>"+"</td>");
            row.append(curr_date);
        }   
        else { 
            var monthplusone = months[month];
            var curr_date = $("<td  class='table-date' id='"+day+"-"+monthplusone+"-"+year+"'>"+day+"</td>");
            var events = check_events(day, month+1, year);
            if(today===day && $(".active-date").length===0) {
                curr_date.addClass("active-date");
                let x = document.getElementById('date').value=day+"-"+monthplusone+"-"+year;
    
                 $('.table-date').ready(function () {
                   x.value;
                  });
                show_events(events, months[month], day);
            }
            // If this date has any events, style it with .event-date
            if(events.length!==0) {
                curr_date.addClass("event-date");
            }
            // Set onClick handler for clicking a date
            $('.table-date').on('click', function () {
              document.getElementById('date').value = $(this).attr('id');
            });
            curr_date.click({events: events, month: months[month], day:day}, date_click);
            row.append(curr_date);
        }
    }
    // Append the last row and set the current year
    calendar_days.append(row);
    $(".year").text(year);
}
gview
  • 14,876
  • 3
  • 46
  • 51
  • 1
    Show what you actually tried, and someone might be able to help you fix it. – Scott Hunter Dec 11 '21 at 21:18
  • 1
    Its a lot of a code . I know it might be hard to understand,but i just needed the logic.Thanks For answering . – Bleron Mexhuani Dec 11 '21 at 21:20
  • 1
    The just show the relevant code. – Scott Hunter Dec 11 '21 at 21:22
  • 1
    Okay i will post it – Bleron Mexhuani Dec 11 '21 at 21:25
  • Just posted a picture for what im looking for . @Scott Hunter – Bleron Mexhuani Dec 11 '21 at 21:35
  • @BleronMexhuani I can only assume. But could it be that your click event handler is reloading the page? That would explain why the onchange trigger does not occur. if yes, then use `preventDefault()` or `return false` function inside you click handler. – Maik Lowrey Dec 11 '21 at 22:02
  • Events are caused by some form of input/user interaction. Events are not triggered by data being changed in javascript. Javascript UI frameworks have these sort of data binding concepts, but you are not using one of those. If you look at your jquery based control, it implements its own click handler. You can add code into that before/after the `document.getElementById('date').value = $(this).attr('id');` to check/do something based on a difference between those 2 values (original/new) – gview Dec 11 '21 at 22:07

5 Answers5

0

This works. Not sure where you're running into an issue.

 function changed(){
    console.log("hello world");
 }
 <input type="text" id="date" class="date" onchange="changed()" name="" >

EDIT: Shortened version of init_calender() for others interested in answering:

function setDate() {
    document.getElementById("date").value = '19-Dec-2021'
}
Spankied
  • 1,626
  • 13
  • 21
  • If you change the value from input yes it works .But when its changed from javascript its actually not working . – Bleron Mexhuani Dec 11 '21 at 21:42
  • You're going to need to shorten your init_calendar() function to only do simple update. Don't have attention to read through all your code. – Spankied Dec 11 '21 at 21:44
  • Please just see the picture ,i think it will help you understand my problem .The value input is not focused when it changing . – Bleron Mexhuani Dec 11 '21 at 21:46
  • I understand, but I don't know exactly how you are changing/setting the value of the input and I don't want to read through your code that does other 50 steps besides setting the value of an input. Help me, help you by distilling your question down to simplest form. – Spankied Dec 11 '21 at 21:49
  • Okay please let me explain it . This main js code creates all td with day month and year and it gives the id of the day month and year , When i click on of them it parses the id value on the (#date) input. Hope you understood it . – Bleron Mexhuani Dec 11 '21 at 21:51
  • Don't explain, change code. No one wants to read through a wall-of-text. Making a SO post is a last resort to get help on an issue. Make it easy for helpers. – Spankied Dec 11 '21 at 21:55
  • I updated my answer with a shortened version of your initCalender(), but I don't feel like helping any further at this point. – Spankied Dec 11 '21 at 22:00
0

Notice that change is actually triggered when the input is not focused anymore.

document.getElementById("date").addEventListener("change", function () {
        alert("hello world");
    });
<input type="text" id="date" class="date" name="">
abendevs
  • 136
  • 1
  • 11
0

I basically agree with @Spankied in that you should try and shorten your code to the point where you are having the issue. However, after looking at your code it seems to me that you want the following function

$('.table-date').on('click', function () {
  document.getElementById('date').value = $(this).attr('id');
});

to not only change the value in your #date input but also trigger its change event-handler function. You can do that by changing it to something like

$('.table-date').on('click', function () {
  document.getElementById('date').value = $(this).attr('id');
  $("#date" ).change();
});

jQuery.change() without any arguments will trigger a predefined "change"-event on the DOM-object that is selected by the jQuery-object.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

You can use js to do that:

let x = $(...) //select the input box
let val = x.value;
function repeat() {
if (val !== x.value) {
change()
}
}
setInterval(repeat, 100)

This checks if the result is the same.

Hermanboxcar
  • 418
  • 1
  • 13
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 12 '21 at 00:12
0

This might make your site a bit slow and it might look odd but this will work in just every case

<script>
    let Oldvalue = $('.date')[0].val();
    setInterval(() => {
         let currentValue = $('.data')[0].val()
         if (Oldvalue != currentValue){ 
             //do whatever but in end write this 
             Oldvalue = currentValue;
         }
    }, 10);
</script>