javascriptjqueryattributesseparatorcode-separation

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!


Solution

  • 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>