Reputation: 133
Hi there can I get code to get a list dropdown of elements in a form by name and also rename them at the same time thanks
Upvotes: 8
Views: 14979
Reputation: 1790
Get values of each drop-down list with common name using jquery?
function checkId(){
$.each($("select[name='selectCtrl']"), function(){
alert($(this).attr('id') +" : "+ $(this).val());
});
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<select class="form-control" id="select1" name="selectCtrl">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<select class="form-control" id="select2" name="selectCtrl">
<option value="4">four</option>
<option value="5">five</option>
<option value="6">six</option>
</select>
<button onclick="checkId()" >check Id</button>
</body>
</html>
Upvotes: 0
Reputation: 1277
you should use the attribute selector
$('td[name=myname]') // matches exactly 'myname'
$('td[name^=myname]') // matches those that begin with 'myname'
Upvotes: 0
Reputation: 944534
jQuery('#myForm select[name=foo]').each(function () {
jQuery(this).attr('name', 'bar')
});
Upvotes: 12