SomeDude SomeDude - 1 month ago 6
jQuery Question

input value not getting read by jQuery

I've spent days looking for an error in PHP only to discover my jQuery was never passing the correct value via AJAX in the first place. I was 100% sure this worked and have tested in the past:

JS:

var barcode = null;
if ($("#barcode").length)
{ var $barcode = $("#barcode").val(); console.log("barcode"); }
alert(barcode);


HTML:

<div class="col-lg-10">
<input type="text" class="form-control" id="barcode" placeholder="Barcode">
</div>


Why does barcode still equal null when there are values in the barcode input?

Answer

Because the variable you are assigning a value to is

$barcode

and you alert

barcode

Like in real life $ matters

Comments