I am having difficulties working with variables in jQuery. The var would need to return the width of the window. To test this I wrote some code where the width is displayed in a paragraph when I hit a button:
var update = function() {
width = $(window).width();
}
$(document).ready(
function() {
$('button').click(
function() {
update,
$('#width').text(width)
}
)
}
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="width">Width</p>
<button>test</button>
I get "[object HTMLParagraphElement]" instead of the value of the width. I don't understand why, because when I run this code:
var width = $(window).width();
$(document).ready(
function () {
$('button').click(
function () {
$('#width').text(width)
}
)
}
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="width">Width</p>
<button>test</button>
I do get the value without any problems. But in this way I can't update the variable after resizing the window (I think because the variable is only declared at the loading of the page.)
This probably is a real newbe problem, but I am very eager to learn...