2

I have a select dropdown which is dynamically populated with ovpn files so it shows, for example, United Kingdom.ovpn

I would like to hide the .ovpn file extension but I haven't been able to using the script below which seems to work on regular html.

The form element:

<select class="selectpicker1" name="ovpn-filename" id="ovpn-filename"></select>

The script I've used to try and hide the file extension (.ovpn), it's currently attached to a button :

<button onclick="myFunction()">Hide Extension</button>

<script>
function myFunction()
{
var str=document.getElementById("ovpn-filename").innerHTML; 
var n=str.replace(".ovpn"," ");
document.getElementById("ovpn-filename").innerHTML=n;
}
</script>
Simon Net
  • 61
  • 3

3 Answers3

0
var str = 'test.ovpn'; alert(str.slice(0, -5));

result is "test" remove last 5 charecters try this concept....change like u...

Chandhru Sekar
  • 308
  • 2
  • 9
0

try this

<button onclick="myFunction()">Hide Extension</button>

<script>
function myFunction()
{
var str=document.getElementById("ovpn-filename").innerHTML; 
var n=str.replace(/.ovpn/g," ");
document.getElementById("ovpn-filename").innerHTML=n;
}
</script>

problem is when you use

var n=str.replace(".ovpn"," ");

it will only replace your 1st occurrence of .ovpn

Abhinav
  • 1,202
  • 1
  • 8
  • 12
0

You can achieve it with jquery:

 <script>
    var optionString = '';  // declare a string to hold all options
    $("#ovpn-filename option").each(function()
    {
        var str=$(this).val(); 
        var n=str.replace(".ovpn"," ");      //remove .ovpn string        
        optionString +=  '<option>'+n+'</option>';  //add back to that option 
    });
    $('#ovpn-filename').html(optionString );  //assign all option html back to select
    </script> 
Bhaskar Jain
  • 1,651
  • 1
  • 12
  • 20
  • Thanks, I'd like this approach but I'm struggling to make it work. I've amended my question with the script which populates the dropdown options as I think modifying it with the approach you have here will provide the solution but I'm not sure exactly what I'm doing wrong. – Simon Net Mar 08 '17 at 14:13
  • Add this code in your button click function, first test it simple without on button click – Bhaskar Jain Mar 08 '17 at 14:16