jquery - find elements having custom attribute with some specific value and replace it -
i want find element has attribute data-socketid
value abc
, replace value xyz
. how this.
please note abc , xyz both dynamically created when page refreshed
this have done far. not working.
html
<div id="chat-win" data-socketid="abc"> <div class="close-conversation-box"> <div class="text-right"> <a id="end-chat" data-socketid="abc" >end chat</a> <a id="stop_bot" data-socketid="abc" >stop bot</a> </div> </div> </div>
script
var matchdatasocketid = $("[data-socketid$=abc]"); var len = object.keys(matchdatasocketid).length; for(var = 0; < len; i++) { $(matchdatasocketid[i]).attr('data-socketid', $(matchdatasocketid[i]).attr('data-socketid').replace(abc, xyz)); }
any help, suggestion highly appreciated. in advance.
as per code sampleimplies it's partial match/replacement, can use .attr(fn)
update attribute value
$("[data-socketid=$abc]").attr(function(_, value) { return value.replace('abc', 'xyz'); });
Comments
Post a Comment