Jump to content

Recommended Posts

Hi,

 

I currently have a working javascript calender pop up that opens a pop up box when i click inside the text box. The class is currently on the text field. I have tried adding it to the image but that does not work.

 

Instead i would like to add the class to the image icon and have the calender to open up when i click on the calender icon image.

 

Can this be done?

 

--------------------------------

 

Here is my code:

 

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Javascript Calendar</title>

<script src="calendar.js" type="text/javascript" language="javascript"></script>

<style type="text/css" media="screen,projection">

@import url(calendar.css);

</style>

</head>

 

<body>

<form action="getDateFunction.php" method="post">

Date Select 1: <input type="text" name="date1" class="calendarSelectDate" />

<img src="images/Calendar-icon.png" alt="" width="16" height="16" border="0" />

<div id="calendarDiv"></div>

 

<p>

<label>

<input type="submit" name="submit" id="submit" value="Submit" />

</label>

</p>

</form>

 

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/187119-adding-a-class-to-a-image/
Share on other sites

 

var popUpCal = {

    selectedMonth: new Date().getMonth(), // 0-11

    selectedYear: new Date().getFullYear(), // 4-digit year

    selectedDay: new Date().getDate(),

    calendarId: 'calendarDiv',

    inputClass: 'calendarSelectDate',

   

init: function () {

        var x = getElementsByClass(popUpCal.inputClass, document, 'input');

        var y = document.getElementById(popUpCal.calendarId);

        // set the calendar position based on the input position

        for (var i=0; i<x.length; i++) {

            x.onfocus = function () {

popUpCal.selectedMonth = new Date().getMonth();

                setPos(this, y); // setPos(targetObj,moveObj)

                y.style.display = 'block';

                popUpCal.drawCalendar(this);

                popUpCal.setupLinks(this);

            }

        }

    },

   

    drawCalendar: function (inputObj) {

 

var html = '';

html = '<a id="closeCalender">Close Calendar</a>';

html += '<table cellpadding="0" cellspacing="0" id="linksTable"><tr>';

    html += ' <td><a id="prevMonth"><< Prev</a></td>';

html += ' <td><a id="nextMonth">Next >></a></td>';

html += '</tr></table>';

html += '<table id="calendar" cellpadding="0" cellspacing="0"><tr>';

html += '<th colspan="7" class="calendarHeader">'+getMonthName(popUpCal.selectedMonth)+' '+popUpCal.selectedYear+'</th>';

html += '</tr><tr class="weekDaysTitleRow">';

        var weekDays = new Array('S','M','T','W','T','F','S');

        for (var j=0; j<weekDays.length; j++) {

html += '<td>'+weekDays[j]+'</td>';

        }

 

        var daysInMonth = getDaysInMonth(popUpCal.selectedYear, popUpCal.selectedMonth);

        var startDay = getFirstDayofMonth(popUpCal.selectedYear, popUpCal.selectedMonth);

        var numRows = 0;

        var printDate = 1;

        if (startDay != 7) {

            numRows = Math.ceil(((startDay+1)+(daysInMonth))/7); // calculate the number of rows to generate

        }

 

        // calculate number of days before calendar starts

        if (startDay != 7) {

            var noPrintDays = startDay + 1;

        } else {

            var noPrintDays = 0; // if sunday print right away

        }

var today = new Date().getDate();

var thisMonth = new Date().getMonth();

var thisYear = new Date().getFullYear();

        // create calendar rows

        for (var e=0; e<numRows; e++) {

html += '<tr class="weekDaysRow">';

            // create calendar days

            for (var f=0; f<7; f++) {

if ( (printDate == today)

&& (popUpCal.selectedYear == thisYear)

&& (popUpCal.selectedMonth == thisMonth)

&& (noPrintDays == 0)) {

html += '<td id="today" class="weekDaysCell">';

} else {

                html += '<td class="weekDaysCell">';

}

                if (noPrintDays == 0) {

if (printDate <= daysInMonth) {

html += '<a>'+printDate+'</a>';

}

                    printDate++;

                }

                html += '</td>';

                if(noPrintDays > 0) noPrintDays--;

            }

            html += '</tr>';

        }

html += '</table>';

       

        // add calendar to element to calendar Div

        var calendarDiv = document.getElementById(popUpCal.calendarId);

        calendarDiv.innerHTML = html;

       

        // close button link

        document.getElementById('closeCalender').onclick = function () {

            calendarDiv.style.display = 'none';

        }

// setup next and previous links

        document.getElementById('prevMonth').onclick = function () {

            popUpCal.selectedMonth--;

            if (popUpCal.selectedMonth < 0) {

                popUpCal.selectedMonth = 11;

                popUpCal.selectedYear--;

            }

            popUpCal.drawCalendar(inputObj);

            popUpCal.setupLinks(inputObj);

        }

        document.getElementById('nextMonth').onclick = function () {

            popUpCal.selectedMonth++;

            if (popUpCal.selectedMonth > 11) {

                popUpCal.selectedMonth = 0;

                popUpCal.selectedYear++;

            }

            popUpCal.drawCalendar(inputObj);

            popUpCal.setupLinks(inputObj);

        }

       

    }, // end drawCalendar function

   

    setupLinks: function (inputObj) {

        // set up link events on calendar table

        var y = document.getElementById('calendar');

        var x = y.getElementsByTagName('a');

        for (var i=0; i<x.length; i++) {

            x.onmouseover = function () {

                this.parentNode.className = 'weekDaysCellOver';

            }

            x.onmouseout = function () {

                this.parentNode.className = 'weekDaysCell';

            }

            x.onclick = function () {

                document.getElementById(popUpCal.calendarId).style.display = 'none';

                popUpCal.selectedDay = this.innerHTML;

                inputObj.value = formatDate(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear);

            }

        }

    }

   

}

// Add calendar event that has wide browser support

if ( typeof window.addEventListener != "undefined" )

    window.addEventListener( "load", popUpCal.init, false );

else if ( typeof window.attachEvent != "undefined" )

    window.attachEvent( "onload", popUpCal.init );

else {

    if ( window.onload != null ) {

        var oldOnload = window.onload;

        window.onload = function ( e ) {

            oldOnload( e );

            popUpCal.init();

        };

    }

    else

        window.onload = popUpCal.init;

}

 

/* Functions Dealing with Dates */

 

function formatDate(Day, Month, Year) {

    Month++; // adjust javascript month

    if (Month <10) Month = '0'+Month; // add a zero if less than 10

    if (Day < 10) Day = '0'+Day; // add a zero if less than 10

    var dateString = Day+'/'+Month+'/'+Year;

    return dateString;

}

 

function getMonthName(month) {

    var monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

    return monthNames[month];

}

 

function getDayName(day) {

    var dayNames = new Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')

    return dayNames[day];

}

 

function getDaysInMonth(year, month) {

    return 32 - new Date(year, month, 32).getDate();

}

 

function getFirstDayofMonth(year, month) {

    var day;

    day = new Date(year, month, 0).getDay();

    return day;

}

 

/* Common Scripts */

 

function getElementsByClass(searchClass,node,tag) {

    var classElements = new Array();

    if ( node == null ) node = document;

    if ( tag == null ) tag = '*';

    var els = node.getElementsByTagName(tag);

    var elsLen = els.length;

    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");

    for (i = 0, j = 0; i < elsLen; i++) {

        if ( pattern.test(els.className) ) {

            classElements[j] = els;

            j++;

        }

    }

    return classElements;

}

 

/* Position Functions */

 

function setPos(targetObj,moveObj) {

    var coors = findPos(targetObj);

    moveObj.style.position = 'absolute';

    moveObj.style.top = coors[1]+20 + 'px';

    moveObj.style.left = coors[0] + 'px';

}

 

function findPos(obj) {

    var curleft = curtop = 0;

    if (obj.offsetParent) {

        curleft = obj.offsetLeft

        curtop = obj.offsetTop

        while (obj = obj.offsetParent) {

            curleft += obj.offsetLeft

            curtop += obj.offsetTop

        }

    }

    return [curleft,curtop];

}

Replace:

 

var x = getElementsByClass(popUpCal.inputClass, document, 'input');

 

With:

 

var x = getElementsByClass(popUpCal.inputClass, document);

 

...And it should work. The third parameter there is restricting the calendar to inputs only.

Ah of course. Well this script wasn't really written to be added to an image, however you could modify it a bit to suit your needs. The first idea that comes to mind, and this is pretty hacky, would be to use the 'rel' attribute to target the input on none-input elements. This line:

 

inputObj.value = formatDate(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear);

 

Is what assigns the value to the input, you could add in a conditional to validate that it's an input, else check if there's 'rel' attribute set, and if so try to use that.

 

Give this a try (I can't say for sure it will work):

 

var dateVal = formatDate(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear);

if (inputObj.tagName != 'input') {
    if (inputObj.getAttribute('rel') != 'undefined') {
        document.getElementById(inputObj.getAttribute('rel')).value = dateVal;
    }
} else {
    inputObj.value = dateVal;
}

 

Remember you'll need to add the id of the input to the rel attribute of the image.

 

If that all made sense?

Basically we're trying to use the rel attribute to reference the ID of an input we're going to write the value to:

 

Date Select 1: <input type="text" name="date1" id="input_id" />

<img src="images/Calendar-icon.png" alt="" width="16" height="16" border="0" class="calendarSelectDate" rel="input_id" />

 

It's a bit messy but I can't see any reason why it wouldn't work.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.