-1
document.getElementById("link1").addEventListener("click", c);
    function c(){
        var a = document.querySelectorAll('.images');
        for(var k in a){
            a[k].setAttribute('class','col-md-12');
        }
    }
document.getElementById("link2").addEventListener("click", d);
    function d(){
        var a = document.querySelectorAll('.images');
        for(var k in a){
            a[k].setAttribute('class','col-md-6');
        }
    }   
document.getElementById("link3").addEventListener("click", e);
    function e(){
        var y = document.querySelectorAll('.images');
        for(var z in y){
            y[z].setAttribute('class','col-md-4');
        }
    }   

here is my JS code , i dont know what im wrong ? anyone can help ? thanks a lot:) my demo in codepen http://codepen.io/anon/pen/MyRmGy

duy khanh
  • 191
  • 1
  • 5
  • 15
  • 4
    Your question should always state what the code is SUPPOSED to do, vs what it ACTUALLY does. – Jeremy J Starcher May 14 '16 at 07:25
  • Hi and welcome to SO! Please read [this](http://stackoverflow.com/help/how-to-ask) post for how to ask better questions, which will help people give you better answers. – Steve Heim May 14 '16 at 10:47

1 Answers1

1

By changing the class attribute of the images, they will not be found by subsequent calls to document.querySelectorAll('.images')

Set a class attribute that maintains the images class:

a[k].setAttribute('class', 'col-md-12 images');

(If your issue is that the code only works on first link click; you haven't said)

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • omg , its working , thanks you very very much :D , do you have any course for learning this ? can you share xD thanks you :D – duy khanh May 14 '16 at 07:54
  • @duy I don't have a course, I've just been working with this for ages, and have made most mistakes myself, so after a while you learn to debug your code. If you have no errors in the console, first step is to verify that you're working with the data you think you are working with. First thing I tried here was to throw in an `alert(a.length)` to check that the events were triggered, and the images were being found. When that gave `10` on first click and `0` on the second, you have a pretty good idea of where the problem is. – David Hedlund May 14 '16 at 09:04