How to change href by select and onchange using javascript

Asked

Viewed 705 times

2

My code is to change the url of 'href' every time the user changes the select. I did once to alter the 'src' of an image, but when I used href it just didn’t work out!

<script language="javascript" type="text/javascript">
  function edit_music()
  {
    if (document.getElementById(\'select1\').value == "0") {
      document.preview1.style.visibility = "hidden";
      document.getElementById(\'random1\').style.visibility = "visible";
    } else {
      var selected = document.getElementById(\'select1\').options[document.getElementById(\'select1\').selectedIndex].value;
      document.preview1.style.visibility = "visible";
      document.preview1.href = "vinhetas/"+selected;
      document.getElementById(\'random1\').style.visibility = "hidden";
    }
  }
</script>

Html:

<select id='select1' name='vinhetas' onchange='edit_music()'>
<option value='$arquivos'>".$arquivos."</option>
</select>
<a href="" name="preview1" id="preview1">Pré-ouvir</a>

What am I doing wrong?

1 answer

1

I got friends! I did something basic:

<script type="text/javascript">
    window.onload = function(){

        mudaLink();
    }

    function mudaLink(){

             var selected  = document.getElementById(\'lista\').value
             document.getElementById(\'link-musical\').href = "vinhetas/"+selected+"";

                }
</script>

and applied to HTML:

<select id=\"lista\" onchange=\"return mudaLink();\" name='vinhetas'>
<option value='$arquivos'>".$arquivos."</option>
</select>
<a id="link-musical" href="teste.png" alt="" width="100" height="100" />Link</a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.