/    Sign up×
Community /Pin to ProfileBookmark

How to add the attribute “data-no-swup”?

Hello, how can I add the “data-no-swup” attribute to all links on the page on JS?
Except for links that have a class “active”!

Example:
This
`
<a href=”#”>01</a>
<a class=”active” href=”#”>01</a>
<a href=”#”>01</a>
<a href=”#”>01</a>
`

To This
`
<a data-no-swup href=”#”>01</a>
<a class=”active” href=”#”>01</a>
<a data-no-swup href=”#”>01</a>
<a data-no-swup href=”#”>01</a>
`

Thanks!

to post a comment
HTMLJavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinSep 17.2020 — <script>

var a=document.querySelectorAll('a');

for(i=0;i<a.length;i++){

if (!a[i].classList.contains('active'))

a[i].setAttribute("data-no-swup","");

}

console.log(a);

</script>





read this to learn other ways to set and read data attribute ...

https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
Copy linkTweet thisAlerts:
@SempervivumSep 17.2020 — @DaveyErwin#1623363 Hi Davey, unfortunately the forum software has eaten your `[i]</C>. You better use code tags when posting code <C>your code here`.
Copy linkTweet thisAlerts:
@daveyerwinSep 17.2020 — ``<i>
</i>&lt;body&gt;
&lt;a href="#"&gt;01&lt;/a&gt;
&lt;a class="active" href="#"&gt;01&lt;/a&gt;
&lt;a href="#"&gt;01&lt;/a&gt;
&lt;a href="#"&gt;01&lt;/a&gt;
&lt;/body&gt;




&lt;script&gt;
var a=document.querySelectorAll('a');
for(i=0;i&lt;a.length;i++){
if (!a[i].classList.contains('active'))
a[i].setAttribute("data-no-swup","");
}
console.log(a);
&lt;/script&gt;<i>
</i>
``

read this to learn other ways to set and read data attribute ...

https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
Copy linkTweet thisAlerts:
@hutizetiSep 17.2020 — thanks ;)
Copy linkTweet thisAlerts:
@etherealauthorSep 17.2020 — @DaveyErwin#1623375 Thank you so much!
×

Success!

Help @ethereal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...