Skip to content
Advertisement

Set Button id from javascript

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

<select name="customer" onchange="getCustomer()"  id="customer" class="form-control" style="width: 180px !IMPORTANT;">
 <option value="">--Select Customer--</option>
                                 <?php
                                 $sql = mysqli_query($db,"SELECT * FROM customer ");
                                 while($row= mysqli_fetch_array($sql))
                                 {
                                 ?>
                                 <option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
                                 <?php
                               }
                                 ?>

 </select>
                                    </div>
                                    <button name="custbt" id="1" class="label-text col-form-label userinfo"><a href="#vCenteredModal" data-toggle="modal">Show customer details</a></button> 

  <script>
          function getCustomer()
          {
            var e = document.getElementById("customer");
            var cust = e.options[e.selectedIndex].value;
          //  alert(cust);custbt
            //document.getElementByName("custbt").id=cust;
            document.getElementByName(custbt).id=cust;

          }
    </script>

Advertisement

Answer

Might be this will solve your problem. I have added a class on btn call CustomerModalBttn .

Try this

<select name="customer" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
<option value="">--Select Customer--</option>
  <?php
  $sql = mysqli_query($db,"SELECT * FROM customer ");
  while($row= mysqli_fetch_array($sql)){
  ?>
     <option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
  <?php
  }
  ?>
</select>

<button name="custbt" id="1" class="label-text col-form-label userinfo CustomerModalBttn">
  <a href="#vCenteredModal" data-toggle="modal">Show customer details</a>
</button> 

<script>
  $(document).ready(function(){
     $('#customer').on('change',function(){
        $('.CustomerModalBttn').attr('id',$(this).val());
     });
  });
</script>
Advertisement