I want to declare a function and use it into other functions. In this example the called function shows the alert but doesn't change the background color. But if I declaire the function inside the $('.box').click(function() {... then it works properly.
What am I doing wrong? How can I declaire a function globally?
$(document).ready(function() {
function changeColor() {
$(this).css('background-color','red');
alert('Color changed!');
}
$('.box').click(function() {
changeColor();
})
});
.box {
width: 100px;
height: 100px;
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box"></div>