简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM