morizvonlanga morizvonlanga - 1 year ago 64
jQuery Question

jQuery - replace attribute value with new value of all elements

I've this html markup:

<div class="item" path="asaa/basba/asbsaa"></div>
<div class="item" path="bsbab/gbfssv/vsaava"></div>
<div class="item" path="asbaba/bsaba/dfsavava"></div>


The important point is that each is different. I need to get this attributes with something like that that:

var title = $(this).attr('path');

Now I need to separate the attribute: I need only the content which is between the * and the *:

<div class="item" path="asaa/basba/*asbsaa*"></div>
<div class="item" path="bsbab/gbfssv/*vsaava*"></div>
<div class="item" path="asbaba/bsaba/*dfsavava*"></div>


Any ideas to do something like that? Thanks for answers!

Answer Source

Get the attribute of each element and then setting back the last part of it

    $(".item").attr("path",function(){         
       return $(this).attr("path").split("/").pop();        
    })
    console.log($('body').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="item" path="asaa/basba/asbsaa"></div>
<div class="item" path="bsbab/gbfssv/vsaava"></div>
<div class="item" path="asbaba/bsaba/dfsavava"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download