i have a selection field and when the button #btnaddtocart is selected i want to be able to check if the selection (name=quantity) isnt null, if it is then an alert is shown to the user. otherwise not to show anything. the script works but the problem is that if the selection does contain a value the alert still shows. why is this?
here is the html
<select class="select" id="quantity" name="quantity">
<option selected="selected" value="">select one</option>
<option value="data1">data1</option>
<option value="data2">data2</option>
<option value="data3">data3</option>
</select>
here is the js
$(document).ready(function() {
$("#btnaddcart").click(function()
{
if($("input[name=quantity]").val() == null)
{
var data = $(this).val();
alert(data);
}
});
});
You’re checking the wrong thing, this:
should be this:
A selector like
input[name=...looks for<input>tags with the specifiednameattribute but you want a<select>element; since your<select>has anidattribute, you can just use an ID selector.