I have got the list of elements like this:
JavaScript
x
7
1
<p style="text-align: center;">
2
<b>List textlink</b>
3
<a class="link link--external has-favicon" href="#abc.com/abc" target="_blank" rel="noopener">ANONFILE</a> -
4
<a class="link link--external has-favicon" href="#abc.com/xyz" target="_blank" rel="noopener">GOFILE</a> -
5
<a class="link link--external has-favicon" href="#abc.com/123" target="_blank" rel="noopener">MEGA</a>
6
</p>
7
Textlink is: ANONFILE, GOFILE, MEGA
With Javascript/JQuery/CSS, Is there anyway I can check textlink and could simply only change the element’s CSS if the inner Textlink equals:
- IF textlink equal “ANONFILE” style css
JavaScript
1
4
1
font-size: 9px;
2
color: red;
3
letter-spacing: 0.5px;
4
- IF textlink equal “GOFILE” style css
JavaScript
1
4
1
font-size: 10px;
2
color: yellow;
3
letter-spacing: 0.6px;
4
- IF textlink equal “MEGA” style css
JavaScript
1
4
1
font-size: 11px;
2
color: green;
3
letter-spacing: 0.7px;
4
The only way I know to do this is to get all of the links and loop through them checking to see if the textlink contains the string I am searching for.
Is there a better way to do this without change original code?
Advertisement
Answer
You could write the classes in CSS, then add the .textContent
s as a class, like this:
JavaScript
1
3
1
for (el of document.querySelectorAll('.link')) {
2
el.classList.add(el.textContent.replaceAll(' ', ''));
3
}
JavaScript
1
23
23
1
.ANONFILE {
2
font-size: 9px;
3
color: red;
4
letter-spacing: 0.5px;
5
}
6
7
.GOFILE {
8
font-size: 10px;
9
color: yellow;
10
letter-spacing: 0.6px;
11
}
12
13
.MEGA {
14
font-size: 11px;
15
color: green;
16
letter-spacing: 0.7px;
17
}
18
19
.GOOGLEDRIVE {
20
font-size: 15px;
21
color: blue;
22
letter-spacing: 1.1px;
23
}
JavaScript
1
7
1
<p style="text-align: center;">
2
<b>List textlink</b>
3
<a class="link link--external has-favicon" href="#abc.com/abc" target="_blank" rel="noopener">ANONFILE</a> -
4
<a class="link link--external has-favicon" href="#abc.com/xyz" target="_blank" rel="noopener">GOFILE</a> -
5
<a class="link link--external has-favicon" href="#abc.com/123" target="_blank" rel="noopener">MEGA</a>
6
<a class="link link--external has-favicon" href="#abc.com/123" target="_blank" rel="noopener">GOOGLE DRIVE</a>
7
</p>