1

I want to change a attribute to lots of elements witch all have the same class.

I have lots of "a" tags with a "data-lc-categories= all" attribute.

I want to change that attribute(data-lc-categories) to all the a tags with a class of animation to data-lc-categories = "animation".

My problem is that if I try to select the elements using a id it works using the:

setAttribute('data-lc-categories', 'animation'); // javascript

OR

$('#test1').attr({
    "data-lc-categories": "animation",
    "title": "some title"
}); // jQuery

but when I try with

document.getElementsByClassName(".img_portfolio_animation")
 .setAttribute('data-lc-categories', 'animation');

OR

$('.animation').attr("data-lc-categories": "animation") 

it doesn't work.

<div class="new_filter_btns_container">
    <ul>
         <li id="filter_All" class="filter_btn">All</li>
         <li id="filter_animations" class="filter_btn">Animations</li>
         <li id="filter_interior" class="filter_btn">Interior Renders</li>
    </ul>
</div>

<a id="test1" class="animation all" href="" data-rel="lightcase:myCollection" data-lc-categories="All">Animation</a>
<a class="animation all" href="" data-rel="lightcase:myCollection" data-lc-categories="All">Animation</a>
<a class="animation all" href="" data-rel="lightcase:myCollection" data-lc-categories="All">Animation</a>
<a class="animation all" href="" data-rel="lightcase:myCollection" data-lc-categories="All">Animation</a>
<a class="interior all" href="" data-rel="lightcase:myCollection" data-lc-categories="All">Interior</a>
<a class="interior all" href="" data-rel="lightcase:myCollection" data-lc-categories="All">Interior</a>
<a class="interior all"     href="" data-rel="lightcase:myCollection" data-lc-categories="All">Interior</a>
<a class="interior all" href="" data-rel="lightcase:myCollection" data-lc-categories="All">Interior</a>
1
  • This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. Commented Nov 4, 2019 at 15:44

1 Answer 1

2

The plain JS version doesn't work because getElementsByClassName() returns a nodeList. As such you cannot call setAttribute() on it. You need to loop through the elements individually and call the method on them one by one, something like this:

var elements = document.getElementsByClassName(".img_portfolio_animation");
for (var i = 0; i < elements.length; i++) {
  elements[i].setAttribute('data-lc-categories', 'animation');
}

The jQuery version doesn't work simply because you need to separate arguments with a comma, not a colon:

$('.animation').attr("data-lc-categories", "animation") 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.