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

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -