Jump to content

HELP! Linking code to an image

Featured Replies

 

Hello forum,

I am pretty new to this so I would really appreciate your help. I have the below code:

<select id="dynamic_select">
  <option value="" selected>Pick a Website</option>
  <option value="http://www.google.com">Google</option>
  <option value="http://www.youtube.com">YouTube</option>
  <option value="https://www.gurustop.net">GuruStop.NET</option>
</select>

<script>
    $(function(){
      // bind change event to select
      $('#dynamic_select').on('change', function () {
          var url = $(this).val(); // get selected value
          if (url) { // require a URL
              window.location = url; // redirect
          }
          return false;
      });
    });
</script>

On my page it looks like this: 

Untitled.png.5dbc796ec05da1225db533eefd46d28d.png

My question is, how can I link this code to an image so when you click on said image, the drop-down appears. So you can only see this selection after you press on the image.

Thank you very much in advance for the help!

Here you go..

<html>
<head>
<script type="text/javascript">
function changeBackground( image )
{
 document.getElementById( 'duck' ).style.backgroundImage = "url( " + image + " )";
}
</script>
<style>
#duck { width: 500px; height: 500px; color: red; text-align: center; font-size: 50px; background-image: url( "duck1.jpg" ); }
</style>
</head>
<body>
<select id="duck">
<option onclick="changeBackground( 'duck1.jpg' );">Duck 1</option>
<option onclick="changeBackground( 'duck2.jpg' );">Duck 2</option>
<option onclick="changeBackground( 'duck3.jpg' );">Duck 3</option>
</select>
</body>
</html>

I tried it with Firefox only.. So you will have to check whether it works also in Chrome, IE and Opera by yourself..

 

Edited by Sensei

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.