0

I am trying the remove the last four characters of each dropdown item in a dynamic file list.

I have tried to use javascript spice syntax after reading this thread, but it seems to remove the full string of the other item in the list rather than just characters. list = list.slice(0, -1);

I'm not sure what is wrong with my approach.

function load_list() {
            $("#filename").empty();
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "/cgi-bin/list_cgi?action=get_list",
                success: function(result) {
                    var list = result.file.sort();
                    var current = result.current.split("/").pop();
                    list = list.slice(0, -1);
                    if (list.length == 0) {
                        $("#unavailable").html("<h2>File not found, please upload.</h2>");
                        show_upload();
                        return;
                    }
                    for (var i in list) {
                        $("filename").append("<option>" + list[i] + "</option>");
                                            }
                    $("filename").val(current);


                }
            });
        }
Community
  • 1
  • 1
pdev
  • 3
  • 3
  • 2
    Without knowing the structure of your result, it would be next to impossible for anyone to help you. It seems you are slicing `list` when you should've been slicing either `list[n]` or `list.file[n]`. – Abhitalks Mar 09 '17 at 11:51
  • see my ans, if you have sorted files into your list object it will work for you. – Abhinav Mar 09 '17 at 11:54
  • @Abhitalks The filename is displayed in a select dropdown `` – pdev Mar 09 '17 at 12:52

2 Answers2

1

try this

    $.each(list,function(i){
  list[i].slice(0, -4);
});

assuming you have sorted files into list object ..

or just replace your code

for (var i in list) {
                    $("filename").append("<option>" + list[i] + "</option>");
                                        }

with

$.each(list, function(i){
list[i] = list[i].slice(0,-4);
$("filename").append("<option>" + list[i] + "</option>");
})
Abhinav
  • 1,202
  • 1
  • 8
  • 12
0

You are slicing an Array and not a string.

Try to use slice in this loop instead.

for (var i in list) {
                        list[i] = list[i].slice(0, -4); //remove last 4 characters
                        $("filename").append("<option>" + list[i] + "</option>");
              }

Update: answering the comment,

You can use

for (...) {
        // use value attribute to ensure that the value is used in its entirety.
        $("filename").append("<option value='" + list[i] + "'>" + list[i].slice(0, -4) + "</option>");
}
Niket Pathak
  • 6,323
  • 1
  • 39
  • 51
  • Niket, this worked for removing the slice so it now displays without the characters but it seems to have stopped the form submitting properly. I wonder if this is because it actually removes the characters on form submit meaning that it can't find the file. Do you know if it's possible to just hide the characters? – pdev Mar 09 '17 at 13:10
  • @pdev you're welcome. updated the post to ensure form submit. :) – Niket Pathak Mar 09 '17 at 13:47