1

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!

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
mvlanga
  • 109
  • 8

2 Answers2

2

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>
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

This is a typicall scenario where I would use the "data" tag

<div class="item" data-path="asaa/basba/asbsaa"></div>

script:

 $(".item").data("path").split("/").pop(); 

more info here

ThunD3eR
  • 3,216
  • 5
  • 50
  • 94