EOB
EOB

Reputation: 3085

Using prototype to set an onclick event of a button?

I have this button:

<button id="check_button" name ="check_button" type="button" onclick="setLocation('...')">
    <span>check</span>
</button>

Now I know I can access it using $('check_button') for example. Is there a way to set the setlocation parameter with Prototype?

Upvotes: 1

Views: 15415

Answers (2)

Ebin John
Ebin John

Reputation: 51

Yes,

$('check_button').observe('click',this.setLocation.bind(this,'...'));

setLocation: function(param)
{
alert(param);
}

In this way the param will be always what you pass. " bind " ia a vary powerfull method which holds that ability.

Upvotes: 0

dontGoPlastic
dontGoPlastic

Reputation: 1782

function doButtonClick(event, element) {
  console.log('event');
  console.log('element');

  // here, `element` is a reference to the button you clicked.
  var elementId = element.identify();

  // I'm not sure what setLocation takes in as a parameter, but...
  setLocation(elementId);
}

$('check_button').on('click', 'button', doButtonClick);

Here are the docs for the element.on: http://api.prototypejs.org/dom/Element/prototype/on/

Which is really just a quick way of creating a new Event.Handler: http://api.prototypejs.org/dom/Event/on/

For sure check out that second doc link - super useful stuff.

Upvotes: 3

Related Questions