How to change the href by select and onchange using javascript

2

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

<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?

    
asked by anonymous 20.08.2014 / 09:31

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>

In HTML, I applied:

<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>
    
20.08.2014 / 10:02