Forum Moderators: open

Message Too Old, No Replies

yahoo's YUI buttons

issue with onclick event for buttons

         

willis1480

5:55 pm on Jul 19, 2009 (gmt 0)

10+ Year Member



I am attempting to implament yahoo YUI Buttons on a site of mine and running into some issues. I cannot seem to apply the current onclick event to the new button.
HTML:

<input type='button' onClick='someFunc()' id='myButton' />

JS CODE:

YAHOO.example.init = function () {
YAHOO.util.Event.onDOMReady(function () {
var curEvent = YAHOO.util.Dom.getAttribute('myButton','onclick');
var myBtn = new YAHOO.widget.Button('myButton', { onclick { fn: curEvent } });
});
}();

I am trying to do something very similar to this. Only problem is, the curEvent files on intialization. How to I add this event to the yahoo button object. Ive tried addEventListner(myBtn.get('id'),'click',curEvent), and myBtn.on('click',curEvent)

seriously going nuts over this right now.

Fotiman

1:41 pm on Jul 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're missing a colon in your new Button line. Below is a working example. Note, though, that the best approach would simply be to remove the inline onClick event handler from your markup. Including it in the HTML code is obtrusive (meaning, if you want to change the behavior of the button, that should be controlled in the JavaScript files, but with inline event handlers you need to modify the HTML markup.

Here's a working example based on your code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
<title>Untitled</title>
<!-- Core + Skin CSS -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/button/assets/skins/sam/button.css">
</head>
<body class="yui-skin-sam">
<input type='button' onclick='someFunc()' id='myButton' />
<!-- Scripts -->
<!-- Dependencies -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/element/element-min.js"></script>
<!-- Source file -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/button/button-min.js"></script>
<script type="text/javascript">
function someFunc() {
alert('hello');
}
YAHOO.example.init = function () {
YAHOO.util.Event.onDOMReady(function () {
var curEvent = YAHOO.util.Dom.get('myButton').onclick;
var myBtn = new YAHOO.widget.Button('myButton', { onclick : { fn: curEvent } });
});
}();
</script>
</body>
</html>

And then the unobtrussive approach:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
<title>Untitled</title>
<!-- Core + Skin CSS -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/button/assets/skins/sam/button.css">
</head>
<body class="yui-skin-sam">
<input type='button' id='myButton' />
<!-- Scripts -->
<!-- Dependencies -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/element/element-min.js"></script>
<!-- Source file -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/button/button-min.js"></script>
<script type="text/javascript">
function someFunc() {
alert('hello');
}
YAHOO.example.init = function () {
YAHOO.util.Event.onDOMReady(function () {
var myBtn = new YAHOO.widget.Button('myButton', { onclick : { fn: someFunc } });
});
}();
</script>
</body>
</html>