Reputation: 1751
I have the following html code:
<a class="dropdown-toggle" id="ddaccount" aria-expanded="false">
<i class="fa fa-lock"></i>
<span class="hidden-sm hidden-md hidden-lg">User Account</span>
<span class="caret"></span>
</a>
I need to replace
with USER
I try usint this jQuery code but it insert at then end of a tag not repalce existing
:
$('a#ddaccount').append('USER');
So i need to get this:
<a class="dropdown-toggle" id="ddaccount" aria-expanded="false">
<i class="fa fa-lock"></i> USER
<span class="hidden-sm hidden-md hidden-lg">User Account</span>
<span class="caret"></span>
</a>
And i get this:
<a class="dropdown-toggle" id="ddaccount" aria-expanded="false">
<i class="fa fa-lock"></i>
<span class="hidden-sm hidden-md hidden-lg">User Account</span>
<span class="caret"></span>
USER
</a>
I try using first() after() i element but none works?
Upvotes: 0
Views: 110
Reputation: 4365
Use replace()
:
$('a#ddaccount').html($('a#ddaccount').html().replace(/ /g, ' USER'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="dropdown-toggle" id="ddaccount" aria-expanded="false">
<i class="fa fa-lock"></i>
<span class="hidden-sm hidden-md hidden-lg">User Account</span>
<span class="caret"></span>
</a>
Upvotes: 1
Reputation: 4956
What you are doing is append()
which simply adds the content to the end of the html content.
What you should be doing is get the entire html content and then in it replace
with USER
and then assign it back to the html.
DEMO
$('a#ddaccount').html($('a#ddaccount').html().replace(/ /g, ' USER'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="dropdown-toggle" id="ddaccount" aria-expanded="false">
<i class="fa fa-lock"></i>
<span class="hidden-sm hidden-md hidden-lg">User Account</span>
<span class="caret"></span>
</a>
Upvotes: 1