I'm having problem with the homepage calendar.

Here is an link -

What I what to do is - When the user clicks the date on the calendar, the page will go to the events page (go to news/events tab, scroll down to see the heading called "Events" under the test).

Below javascript pointing to the wrong link. How can I fix the javascript so it points to the correct link?

function showEvents(date) {
rtn = "";
for (var i = 1; i < events.length; i++)
if(1900+date.getYear()==events[i][2] && 1+date.getMonth()==events[i][3] && date.getDate()==events[i][4])
rtn += events[i][1]+", \r\n";
if(rtn == "") return [false, "", ""];
return [true, '',rtn];
}

function callEvent(dateText, inst) {
dateText2 = dateText.split("/"); dateText2 = dateText2[2]+"-"+dateText2[0]+"-"+dateText2[1];
document.location.href="Event-OnDate-"+dateText2+".html";
}

$( "#datepicker" ).datepicker({
beforeShowDay: showEvents,
onSelect: callEvent
});

Any help would be appreciated?

Cheers
Paul

Dani AI

Generated

Short diagnosis: the calendar code is building the wrong link because the onSelect handler is reading the wrong thing and there was also a small query-string typo. wants a date click to open the event detail under the news/events area; already flagged the stray equals sign in the query string (the param should be ?id= not ?=id=). The safer, more maintainable fix is to derive the target URL from the same events data used by the calendar instead of scraping DOM attributes or trying to reformat the displayed date string.

A simple, robust pattern is to store the target link (or id) with each event entry and look it up by date in onSelect. Example structure and lookup:

/* events = [
     { year:2013, month:3, day:27, url:'/path/to/event/59' },
     ...
] */

function onDateSelected(dateText) {
  var d = $('#datepicker').datepicker('getDate'); // Date object
  var y = d.getFullYear(), m = d.getMonth() + 1, day = d.getDate();

  for (var i = 0; i < events.length; i++) {
    var ev = events[i];
    if (ev.year === y && ev.month === m && ev.day === day) {
      window.location.href = ev.url; // ev.url set when events are built
      return;
    }
  }
}

If adding URLs to the events data is not possible, the title/tooltip set by beforeShowDay can be read from the datepicker DOM — but not from the inst object directly. The clicked anchor lives in inst.dpDiv; read its title, extract an ID, and build a safe base path:

function onDateSelectedUsingTooltip(dateText, inst) {
  var title = inst && inst.dpDiv && inst.dpDiv.find('.ui-state-active').attr('title') || '';
  var m = title.match(/\d+$/);
  if (m) window.location.href = '/path/to/news?id=' + m[0];
}

Troubleshooting notes: prefer getDate() to parsing strings (avoids locale issues), log inst in the console to inspect structure, check the jQuery UI version because class names can differ (ui-state-active vs others), and use root-relative or absolute URLs to avoid incorrect relative links. If multiple events share one date, decide whether to redirect to a list page or present a selection.

Recommended Answers

All 5 Replies

Line 12 of your code.

document.location.href="Event-OnDate-"+dateText2+".html";

You could put in front of it what ever the correct url is.

Example:

document.location.href="news/Event-OnDate-"+dateText2+".html";

I couldn't tell the exact change because I am still not sure where you are trying to get it to go.

Hey pixelsoul,

I'm trying to make a point to

Example:

document.location.href="Event-OnDate-"+dateText2+".html";

change to

document.location.href="news1.php?=id="+ $(inst).attr('title').match(/\d+$/);

Is there anything wrong with this script?

The only thing I can see that looks to be a problem is the "=" after the beginning of the query string.

document.location.href="news1.php?id="+ $(inst).attr('title').match(/\d+$/);

Is there any other way to fix the error? Maybe change "=" to "-"??

What error are you getting? You didn't mention any error before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.