I'm trying to improve my understanding of javascript/jQuery function patterns. I've been playing with this simple demo in an attempt to get a revealing module pattern working.
Could anyone help me understand why this isn't working? I know that in reality you would solve just using CSS and that there are easy other ways to solve it - what I'm interested in is why my attempted solution doesn't work.
HTML
<body>
<p>Here is a test input element</p>
<form>
<label>Some label</label>
<input type="text">
<button>Click me</button>
</form>
</body>
</html>
jQuery:
$(document).ready(function(){
var roll = (function(){
function rollEnter(){
$("button", this).css("text-decoration", "underline");
}
function rollExit(){
$("button", this).css("text-decoration", "none");
}
return{
underlined: rollEnter,
standard: rollExit
};
})();
//When I try and call the functions, it doesn't work
$("button").on('mouseenter', roll.underlined());
$("button").on('mouseleave', roll.standard());
});
Any advice on what went wrong/how to get this type of pattern working?
oninstead of the result of executing the function. Also, what does "it doesn't work" mean? Do you see an error on the page?()afterunderlinedandstandard. I think what is happening is you are calling the methods immediately rather than on mouseenter/mouseleave