Elian ten Holder Elian ten Holder - 4 months ago 14
Javascript Question

Return the attribute id from the element that triggerd the event

I have an element, this element calls the function calcTotal via the following code:

$('.pause').change(function(e) {
window.alert("pause changed");
calcTotal(e);


The code of calcTotal(e) is as follows:

function calcTotal(event)
{ alert('calcTotal called');
var myId = event.currentTarget.attr('id');

myId = myId.replace(/[^0-9]/g, '');

var timeRegex = /^[0-9]{1,2}:[0-9]{2}$/;

if($('#start'+myId).val().match(timeRegex) && $('#end'+myId).val().match(timeRegex) && $('#pause'+myId).val().match(timeRegex))
{
var minutes = 0;

var n = $('#end'+myId).val().split(':');
minutes = parseInt(n[0])*60 + parseInt(n[1]);

var n = $('#start'+myId).val().split(':');
minutes -= parseInt(n[0])*60 + parseInt(n[1]);

var n = $('#pause'+myId).val().split(':');
minutes -= parseInt(n[0])*60 + parseInt(n[1]);

var hours = Math.floor(minutes/60);
minutes = minutes % 60;
alert(hours + ':' + minutes);
$('#total' + myId).val(hours + ':' + minutes);
}
else
{
$('#total' + myId).val('00:00');
}

}


It doesn't work as I had exceptected and when I debug with firebug it says the following:

TypeError: event.currentTarget.attr is not a function
var myId = event.currentTarget.attr('id');


I would like to store the id of the element in myId.
How would I do this?

Answer

event.currentTarget is not a jQuery object, it's a DOM node.

var myIf = event.currentTarget.id;