I have some text that I've fetched from an API and need to wrap a word in it, say "Biography", with <span>
and </span>
tags so that I can style it differently from the surrounding text. How I can use jQuery or JavaScript to do this?
Asked
Active
Viewed 360 times
-3

Mark Amery
- 143,130
- 81
- 406
- 459

user5248
- 347
- 1
- 3
- 21
-
Here is an answer https://stackoverflow.com/a/926964/920557 – Eugene Komisarenko Aug 01 '17 at 16:27
2 Answers
2
<!DOCTYPE html>
<html>
<head>
<style>
.big{ font-size: 55px; }
</style>
<script>
function chng(){
var text = document.getElementById('pText').innerHTML;
var text2 = text.replace('Germany','<span class="big">Germany</span>');
document.getElementById('pText').innerHTML = text2;
}
</script>
</head>
<body>
<p id="pText">
Brazil - England - Germany
</p>
<button onclick="chng()">Try it</button>
</body>
</html>

Norberto Alves Filho
- 19
- 3
-
Yes that is exactly the kind of solution I'm looking for, but without having it change on click. Is there anyway to change it automatically on page load? – user5248 Aug 01 '17 at 20:24
-
1
-1
How are the words supposed to be chosen for wrapping (although we don't know what you mean)? It seems you have to do this on your content generation (in PHP, .Net, asp, ect). So you create the classes on css and use them on content generation, something like (php): echo 'This is mytext.'; (css): .big {font-weight: bold; font-size: 40px;}

Norberto Alves Filho
- 19
- 3
-
I'm using a Wordpress plugin to insert Google Sheets data. It comes in in the form of unstyled tables. The tables themselves don't have any specific css, so I cannot target anything specific in the table, unless I inject styling via javascript based on specific words. – user5248 Aug 01 '17 at 17:12
-