Imagine I have multiple span elements like this:
<span>A</span>
<span>B</span>
<span>C</span>
<span>D</span>
and a div element (which will be converted to a button later) named "change".
<div id="change">CHANGE</div>
Is there a way to update the text of all the spans on the page with just one click of a button?
I'm not very experienced with JavaScript and I've attempted this code, but it doesn't seem to be effective.
$(document).ready(function(){
$('#change').click(function(){
$("span").replaceAll(function(){
while ('span') {
if ($('#span').text() == 'A') {
return $('span').text('B');
}
else if ($('span').text() == 'B') {
$('span').text('C');
}
else if ($('span').text() == 'C') {
$('span').text('D');
}
else if ($('span').text() == 'D') {
$('span').text('A');
}
}
});
});
});
Thank you in advance!