JS onchange get current object data

How can i get “data-tid” value in JS function?

PHP
$colA = “<input type=‘date’ class=‘sdate’ data-tid=‘{$r[‘tid’]}’ value=‘$sd’ onchange="elog(‘change’,this);return false;">”

JS
function elog(ev, object) {
let tid = this.data-tid;
console.log(tid + " - " + ev + ": " + object.value);
}

If I am understanding correctly…

You are calling with:

onchange="elog(‘change’,this);return false;"

Won’t that instantly invoke elog?

I would expect something like:

onchange="e => elog(e, 'change', this);"

and then:

function elog(e, eventType, eventThis) {
  // ...
}

And I’m not sure if you need that last parameter.

Or maybe I am misunderstanding.

But How can i get “data-tid” value, show to console.log when someone change the date value?

I don’t do much HTML, but I would expect it to be available in the function on the event, like e.target[‘data-tid’], or something like that.

this does not reference what you think it does (It is actually the Window object). Since the parameter named object will reference the input because you passed this as the second argument of the elog function, you can get the value of tid using:

object.dataset.tid

You could create a more generic version of the elog function by writing it as:

function elog(ev) {
  const elem = ev.target;
  let tid = elem.dataset.tid;
  console.log(tid + " - " + elem.type + ": " + elem.value);
}

Then, your onchange attribute value would be:

onchange="elog(event);return false;"

Writing it this way, you can use the type property of the event object instead of having to explicitly pass change to elog.

you are correct, thanks for your help.
just curious if using jquery, am i correct?

    $().ready( function() {
         $(".sdate").on("change", function() {
             let tid = $(this).data("tid")
             let bid = $(this).data("bid")
             console.log(tid + "." + bid + "Change event" + this.value);
         })
     })

just log the value of this to check