Loop to create variables, function/array loop

3718 views javascript
-4

I coded a program to highlight a selected button when clicked and to un-highlight it when another button is clicked. It is working, but I need to make it less repetitive (using maybe loops).

var txt = $(".txt");
var xls = $(".xls");
var jpg = $(".jpg");
var png = $(".png");
var pdf = $(".pdf");
var mp3 = $(".mp3");
var mp4 = $(".mp4");
var gif = $(".gif");
var flac = $(".flac");
var html = $(".html");
var js = $(".js");
var aac = $(".aac");
var ai = $(".ai");
var avi = $(".avi");
var psd = $(".psd");
var tiff = $(".tiff");

var fileTypes = [txt, xls, jpg, png, pdf, mp3, mp4, gif, flac, html, js, aac, ai, avi, psd, tiff];

$(fileTypes[0]).click( function() {
    for(i=0; i<fileTypes.length; i++) 
    {
        fileTypes[i].removeClass("selected");
        fileTypes[i].addClass("not-selected");
    }

    fileTypes[0].removeClass("not-selected");
    fileTypes[0].addClass("selected");
});

$(fileTypes[1]).click( function() {
    for(i=0; i<fileTypes.length; i++) 
    {
        fileTypes[i].removeClass("selected");
        fileTypes[i].addClass("not-selected");
    }

    fileTypes[1].removeClass("not-selected");
    fileTypes[1].addClass("selected");

});

$(fileTypes[2]).click( function() {
    for(i=0; i<fileTypes.length; i++) 
    {
        fileTypes[i].removeClass("selected");
        fileTypes[i].addClass("not-selected");
    }

    fileTypes[2].removeClass("not-selected");
    fileTypes[2].addClass("selected");

});  // It does this 16 times, the length of the fileTypes array

I have tried using a for loop to solve this problem, but I have been unsuccessful. Please help. Thanks.

answered question

2 Answers

10

Perhaps you could simplify you script by doing something along the lines of the following:

var extensions = ['.txt',
'.xls',
'.jpg',
'.png',
'.pdf',
'.mp3',
'.mp4',
'.gif',
'.flac',
'.html',
'.js',
'.aac',
'.ai',
'.avi',
'.psd',
'.tiff']

// Create a selector that selects all classes in
// the extensions array
var selector = extensions.join(',');

// Assign a click handler to each element in the
// selected string
$(selector).click(function() {

  // When clicked, remove the selected 
  // class, and add not-selected to all 
  // elements in the selector
  $(selector)
  .removeClass('selected')
  .addClass('not-selected')

  // For this item, remove not-selected
  // class and add selected class
  $(this)
  .removeClass('not-selected')
  .addClass('selected')

})
.selected {
background:red;
}

.not-selected {
background:white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button class="txt">txt</button>
<button class="xls">xls</button>
<button class="jpg">jpg</button>
<button class="png">png</button>
<button class="pdf">pdf</button>
<button class="mp3">mp3</button>
<button class="mp4">mp4</button>
<button class="gif">gif</button>
<button class="flac">flac</button>
<button class="html">html</button>
<button class="js">js</button>
<button class="aac">aac</button>
<button class="ai">ai</button>
<button class="avi">avi</button>
<button class="psd">psd</button>
<button class="tiff">tiff</button>

posted this
1

Something like this?

var switch = function(event) {
    for(i=0; i<fileTypes.length; i++) 
    {
        fileTypes[i].switchClass("selected","not-selected");
    }
    $(event.currentTarget).switchClass("not-selected","selected");
}

for(i=0; i<fileTypes.length; i++) 
{
    $(fileTypes[i]).click(switch);
}

I did not test it but it should work. Basically I create an anonymous function assign it to a variable and use it as a handler for clicks on all your objects.

posted this

Have an answer?

JD

Please login first before posting an answer.