undefined undefined - 4 days ago 6
Javascript Question

How to get the date of the selected <td> in a calendar

I need an idea and maybe a solution to get the date from the

<td>
s which are selected by mouse.

Heres my code, to select (until now its only a function to change the class) the TD:

$(function () {
var isMouseDown = false,
isHighlighted;
$("#schichtplan td")
.mousedown(function () {
isMouseDown = true;
$(this).toggleClass("highlight");
isHighlighted = $(this).hasClass("highlight");
return false; // prevent text selection
})
.mouseover(function () {
if (isMouseDown) {
$(this).toggleClass("highlight", isHighlighted);
}
});
$(document)
.mouseup(function () {
isMouseDown = false;
});
});


A screenshot from the calendar:
Screenshot

This is, how the days are displayed:

echo "<td class=\"td kal_standard_tag\"> - </td>";


The "-" is displayed, because at this day is no entry for this user.

Maybe I could add something like this and read it out with jQuery?

timestamp=\"".$timestamp_tag."\"


My target: After selecting the days, the user has to use rightclick and a Dialog shows up where the user can add some informations and save it to a database.
But I dont know how to get the selected days... :(

Does anyone have an idea and maybe a solution for that?

Answer

The best practice to do such things is to add a data attribute. So you would add a timestamp or datestamp as you said. E.g.: <td data-timestamp="1231232131"> and then get it with jQuery in an event with $(this).data('timestamp').

Comments