Home > Mobile >  How to change image when I select dropdown in Bootstrap
How to change image when I select dropdown in Bootstrap

Time:10-12

I have a dropdown list in Bootstrap. How to change img src (<img src="img/website_building.jpg"...) when selecting items in the dropdown list. Is this possible?

<tr>
  <td >
    <div >
      <a  href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Test
      </a>

      <div  aria-labelledby="dropdownMenuLink">
        <a  href="#">1</a>
        <a  href="#">2</a>
        <a  href="#">3</a>
      </div>
    </div>
  </td>
  <td >
    <img src="img/website_building.jpg" rel="popover"  width="300" alt="">
  </td>
  </td>
</tr>

CodePudding user response:

when selecting items in the dropdown list

You can add an event to listen to when the drop down is about to be shown: bootstrap 4 dropdown events

eg:

$('.dropdown')
  .on('show.bs.dropdown', function() {
    // do something
  })
  .on('hide.bs.dropdown', function() {
    // do something
  })

within these events you can change the src attribute of the image.

Here's an example:

var imgOff = "https://i.stack.imgur.com/lxthA.jpg";
var imgOn = "https://i.stack.imgur.com/OVOg3.jpg";

$('.dropdown')
  .on('show.bs.dropdown', function() {
    $(".img-thumbnail").attr("src", imgOn);
  })
  .on('hide.bs.dropdown', function() {
    $(".img-thumbnail").attr("src", imgOff);
  })
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C OGpamoFVy38MVBnE IbbVYUew OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>

<table>

<tr>
  <td >
    <div >
      <a  href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Test
      </a>

      <div  aria-labelledby="dropdownMenuLink">
        <a  href="#">1</a>
        <a  href="#">2</a>
        <a  href="#">3</a>
      </div>
    </div>
  </td>
  <td >
    <img src="https://i.stack.imgur.com/lxthA.jpg" rel="popover"  width="300" alt="">
  </td>
</tr>

</table>

CodePudding user response:

The simplest way is just to create event listeners for the dropdown items and change the URL based on a dictionary or some other source.

For instance, you can create event listeners for the click event as such:

$(".dropdown-item").click(function () {
  console.log('I clicked', this.innerHTML)
});

The next step is to come up with a way to map each dropdown item to the desired image URL. There are a number of ways you can do that, like using dictionaries in which ids of the items are keys and URLs are values. Below I show you an example using data attributes.

$(".dropdown-item").click(function() {
  const imageURL = $(this).data("image");
  $("img").attr("src", imageURL);
});
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>

Select any item in the dropdown below to change the image.

<table>
  <tr>
    <td >

      <div >
        <a  href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Test
  </a>

        <div  aria-labelledby="dropdownMenuLink">
          <a  href="#" data-image="http://bc.id.au/images/it_flag.gif">1</a>
          <a  href="#" data-image="http://bc.id.au/images/fr_flag.gif">2</a>
          <a  href="#" data-image="http://bc.id.au/images/es_flag.gif">3</a>
        </div>
      </div>

    </td>
    <td >
      <img src="http://bc.id.au/images/de_flag.gif" rel="popover"  width="300" alt="">
    </td>
    </td>
  </tr>
</table>

  • Related