PDA

View Full Version : Adding click event to a button



Riddler
1 Dec 2010, 7:34 AM
I would like to add a click events on a button ...



var travelModeButtons = [
{
text: 'A PIEDI',
style: buttonStyle,
autoEvent : true,
listeners:
{
'click': function() { alert("1"); /*calcRoute(start,end,1)*/}
}
},
{
text: 'IN BICI',
hendler: calcRoute(start,end,2),
style: buttonStyle
},
{
text: 'IN MACCHINA',
autoEvent : true,
hendler: calcRoute(start,end,3),
style: buttonStyle
}
];
And in a function I check wich button is clicked


function calcRoute(start, end, wichTravelMode) {
// WALKING BICYCLING DRIVING
var tMode;
alert("!");
switch(wichTravelMode){
case 1:
tMode = google.maps.DirectionsTravelMode.WALKING
break;
case 2:
tMode = google.maps.DirectionsTravelMode.BICYCLING
break;
case 3:
tMode = google.maps.DirectionsTravelMode.DRIVING
break;
default:
tMode = google.maps.DirectionsTravelMode.DRIVING

}
var request = {
origin: start,
destination: end,
travelMode: tMode
};

directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
But the events ... don't do nothing ... which is the error?

thanks to all

--
Riddler ?

steve1964
1 Dec 2010, 10:36 AM
Is handler not hendler...


var travelModeButtons = [
{
text: 'A PIEDI',
style: buttonStyle,
autoEvent : true,
listeners:
{
'click': function() { alert("1"); /*calcRoute(start,end,1)*/}
}
},
{
text: 'IN BICI',
hendler: calcRoute(start,end,2),
style: buttonStyle
},
{
text: 'IN MACCHINA',
autoEvent : true,
hendler: calcRoute(start,end,3),
style: buttonStyle
}
];

Riddler
2 Dec 2010, 12:08 AM
yes yes ... this is my copy/paste error ;-)
but not work equally :-(

steve1964
2 Dec 2010, 12:57 AM
I dont understand why you call a function, why not just:


handler: function(){
tMode = google.maps.DirectionsTravelMode.WALKING;
}

and so on...

Riddler
2 Dec 2010, 1:02 AM
Thanke for yours reply

good idea! I try it immediatly!

Riddler
2 Dec 2010, 1:11 AM
This is the correct call of a function:



{
text: 'IN BICI',
handler: function() {
tMode = google.maps.DirectionsTravelMode.BICYCLING;
calcRoute(start,end)
} ,
style: buttonStyle
},


The handler nust be associated to a function that call a function calcRoute

Thanks to all

--
Riddler ?