Reputation: 1
I am trying to delete RT from the following mark up. I have tried
$('#tweet').text( $('#tweet').text().replace("RT ", ''));
but it only removes the RT from the first div. Any ideas how?
<div class="fslider" data-animation="slide" data-arrows="false" data-pagi="false">
<div class="flexslider">
<div class="slider-wrap">
<div class="slide" id="tweet">RT A new empowerment group, the Business Economic Empowerment Forum, has been launched in Zim aiming to promote business and ec…<span class="twitter_date"><br>
</span></div>
<div class="slide" id="tweet">RT Alexander Forbes extends footprint to Zim through its acquisition of a significant stake in African Actuarial Consultants<span class="twitter_date"><br>
</span></div>
<div class="slide" id="tweet">RT Nigeria &amp; Morocco sign agreements on a regional gas pipeline which will see Nigeria providing gas to countries in West Africa<span class="twitter_date"><br>
</span></div>
</div>
</div>
</div>
Upvotes: 0
Views: 33
Reputation: 1074276
id
values must be unique on the page, so the first step is to change your markup to use a tweet
class instead of id
:
<div class="fslider" data-animation="slide" data-arrows="false" data-pagi="false">
<div class="flexslider">
<div class="slider-wrap">
<div class="slide tweet">RT A new empowerment group, the Business Economic Empowerment Forum, has been launched in Zim aiming to promote business and ec…<span class="twitter_date"><br>
</span></div>
<div class="slide tweet">RT Alexander Forbes extends footprint to Zim through its acquisition of a significant stake in African Actuarial Consultants<span class="twitter_date"><br>
</span></div>
<div class="slide tweet">RT Nigeria &amp; Morocco sign agreements on a regional gas pipeline which will see Nigeria providing gas to countries in West Africa<span class="twitter_date"><br>
</span></div>
</div>
</div>
</div>
Then, you need to loop through them with text
's callback:
$(".tweet").text(function(_, text) {
return text.replace("RT ", "");
});
Note that, bizarrely, the text is the second argument to the callback.
Live Example:
$(".tweet").text(function(_, text) {
return text.replace("RT ", "");
});
<div class="fslider" data-animation="slide" data-arrows="false" data-pagi="false">
<div class="flexslider">
<div class="slider-wrap">
<div class="slide tweet">RT A new empowerment group, the Business Economic Empowerment Forum, has been launched in Zim aiming to promote business and ec…<span class="twitter_date"><br>
</span></div>
<div class="slide tweet">RT Alexander Forbes extends footprint to Zim through its acquisition of a significant stake in African Actuarial Consultants<span class="twitter_date"><br>
</span></div>
<div class="slide tweet">RT Nigeria &amp; Morocco sign agreements on a regional gas pipeline which will see Nigeria providing gas to countries in West Africa<span class="twitter_date"><br>
</span></div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Note that your replace
will replace RT
regardless of where it is in the text, and will replace only the first one. To make it specifically only replace one at the beginning of the text, if any, change it to
return text.replace(/^RT /, "");
...where ^
means "start of input." You might even use /^RT +/
so you replace all spaces following it rather than just one.
Live Example:
$(".tweet").text(function(_, text) {
return text.replace(/^RT +/, "");
});
<div class="fslider" data-animation="slide" data-arrows="false" data-pagi="false">
<div class="flexslider">
<div class="slider-wrap">
<div class="slide tweet">RT A new empowerment group, the Business Economic Empowerment Forum, has been launched in Zim aiming to promote business and ec…<span class="twitter_date"><br>
</span></div>
<div class="slide tweet">RT Alexander Forbes extends footprint to Zim through its acquisition of a significant stake in African Actuarial Consultants<span class="twitter_date"><br>
</span></div>
<div class="slide tweet">RT Nigeria &amp; Morocco sign agreements on a regional gas pipeline which will see Nigeria providing gas to countries in West Africa<span class="twitter_date"><br>
</span></div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Upvotes: 4