I have a dropdown list which filled with the data fetched from db. I need to assign the selected index value of this dropdown list as the ID of a button. Is it really possible?
my code
JavaScript
x
28
28
1
<select name="customer" onchange="getCustomer()" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
2
<option value="">--Select Customer--</option>
3
<?php
4
$sql = mysqli_query($db,"SELECT * FROM customer ");
5
while($row= mysqli_fetch_array($sql))
6
{
7
?>
8
<option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
9
<?php
10
}
11
?>
12
13
</select>
14
</div>
15
<button name="custbt" id="1" class="label-text col-form-label userinfo"><a href="#vCenteredModal" data-toggle="modal">Show customer details</a></button>
16
17
<script>
18
function getCustomer()
19
{
20
var e = document.getElementById("customer");
21
var cust = e.options[e.selectedIndex].value;
22
// alert(cust);custbt
23
//document.getElementByName("custbt").id=cust;
24
document.getElementByName(custbt).id=cust;
25
26
}
27
</script>
28
Advertisement
Answer
Might be this will solve your problem. I have added a class on btn call CustomerModalBttn .
Try this
JavaScript
1
24
24
1
<select name="customer" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
2
<option value="">--Select Customer--</option>
3
<?php
4
$sql = mysqli_query($db,"SELECT * FROM customer ");
5
while($row= mysqli_fetch_array($sql)){
6
?>
7
<option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
8
<?php
9
}
10
?>
11
</select>
12
13
<button name="custbt" id="1" class="label-text col-form-label userinfo CustomerModalBttn">
14
<a href="#vCenteredModal" data-toggle="modal">Show customer details</a>
15
</button>
16
17
<script>
18
$(document).ready(function(){
19
$('#customer').on('change',function(){
20
$('.CustomerModalBttn').attr('id',$(this).val());
21
});
22
});
23
</script>
24