Search code examples
javascriptjqueryarraysattr

How to use attr in elements within an array?


So, I have an array with 200 items called "icone", and when I use this code:

icone.attr('src', 'img/known.svg');

All items change according to what is in the code. But how do I do if I want to change only 1 of the items in this array, and not all 200?

I tried with:

icone[0].attr('src', 'img/known.svg');

But the console returned "icone[0].attr is not a function".

Any ideas on how to make this work? Thanks!


Solution

  • If it is absolutely necessary to use jQuery in this case. You can do this:

    $(icone[0]).attr('src', 'img/known.svg');
    

    In case it is not, you can do it as the other answers say.