I have this radio button, if checked i want it to display 2 alerts showing ( item="shoes" ) one from local and the other from global , the fact that the alert shows only one means that the variable is still empty on the global.
/*Empty item*/
var item = '';
/*Function selection item*/
function selection(select) {
var item = select;
alert(item);
}
/*If radio button selected item='shoes'*/
$('input[name="selection"]').click(function() {
if ($(this).is(':checked')) {
selection('shoes');
}
});
/*Alert on item if not empty*/
if (item !== '') {
alert(item); /*NOT WORKING*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<input type="radio" name="selection" id="shoes" value="shoes">Shoes<br>
How to transfer a variable from local to global scope ? please don't mark me as a dulicate only if you're sure that another subject will solve my problem. Thanks
itemin functionselectionand the global one are two differnt variables, because of thevarin front ofvar item = select. But even if you remove thevarthe secondalertwould not be called, becaue theifwill be evaluated before the click happens.