$(document).ready(function() {
var v = $("input[name=link]").val();
$('p').on('click', function () {
console.log(v);
console.log('hello');
})
});
Above is a jQuery/JS code that I have written. It is supposed to get the value in an input field, and print it to the console when i click on a paragraph.
I already declare v as a global variable, but it's not printing to the console. It only works if I place the var v declaration into the function making it local variable. Why it is like this? I thought global variables are always supposed to work all around the whole code. Pls educate me on this.