Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$('#select').val() returns UNDEFINED

I have written some jQuery code, this is a part of it. this always return UNDEFINED. why is that

<html>
<head>
<script src="theme/js/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
   $("#my").click(function() {
        alert($("#adults").val());
    });
 }); 

</script>
</head>
<body>

<form>
<a href="#" id="my">click</a>
<select class="text" id="currency" id="adults">
    <option value="1"  selected="selected">1</option>
    <option value="2" >2</option>
    <option value="3" >3</option>
    <option value="4" >4</option>
</select>
</form>
</body>
</html>

This is used to get how many adults are there. Can't run without it.

like image 363
lankitha Avatar asked Dec 18 '25 18:12

lankitha


1 Answers

You can't use id attribute two times on one node (in fact id values should be unique across one html page). In your case use either currency or adults.

If you get rid of extra id="currency" jQuery will return correct value using expression $("#adults").val().

like image 139
WTK Avatar answered Dec 21 '25 08:12

WTK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!