Reputation: 3
Hello guys i was trying to add a new option to the select using ajax but its not working and i have no idea why,,
<select name="category" id="category" class="form-control multi_select" placeholder="Select category" required>
<option> Op1 </option>
</select>
$(function (){
$('.edit_click').click(function (e){
e.preventDefault();
$('#editModal').modal('show');
$('#category').append(`<option value="test">Test</option>`);
});
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
i tried to use every code in the enternet and nothing was working but everything else is working in ajax
Upvotes: 0
Views: 301
Reputation: 717
Its working! It depends where you have put your code. It should be after the jQuery library loaded
$(function (){
$('.edit_click').click(function (e){
e.preventDefault();
$('#category').append(`<option value="test">Test</option>`);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<select name="category" id="category" class="form-control multi_select" placeholder="Select category" required>
<option> Op1 </option>
</select>
<a href="javascript:void(0);" class="edit_click">Edit</a>
Upvotes: 1