2

I have a very large HTML that contains lots of divs with the same name, I want a way to only filter or extract that value from that div.

Here is an example:

<td class="last">
  <div class="container-relative">
    <div class="name" title="&quot;User&quot; <John Appleseed>"></div>
    <div class="date">9/17/2019</div>
    <div class="tool"></div>
  </div>
</td>

I need to extract only what's between <John Appleseed>, in this case is 'John Appleseed'.

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
  • 1
    `value` ? You mean the title of the div ? So you have a lot of `div` with same `class` attribute but different `title` attributes ? And you want to get all those title attributes values ? And do what with them ? – Mihai T Sep 23 '19 at 08:43
  • `getAttribute`. That's what you're looking for – Sagar V Sep 23 '19 at 08:43
  • Possible duplicate of [Find an element in DOM based on an attribute value](https://stackoverflow.com/questions/2694640/find-an-element-in-dom-based-on-an-attribute-value) – Alon Yampolski Sep 23 '19 at 08:45
  • @MihaiT yes, that's correct I want to extract the title. The title is different for each div. – Kayla tucker Sep 23 '19 at 08:56

4 Answers4

3

You could use querySelectorAll to take all the elements with class name, then get the title attribute with getAttribute, and finally use a regular expression to match text between <>.

document.querySelectorAll('.name').forEach(item => {
  let title = item.getAttribute('title');
  console.log(title.match(/\<.*\>/));
});
<td class="last">
  <div class="container-relative">
    <div class="name" title="&quot;User&quot; <John Appleseed>"></div>
    <div class="date">9/17/2019</div>
    <div class="tool"></div>
  </div>
</td>
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
0
var divs=[];
for(i=0,j=0,obj=document.getElementsByClassName("name");i<obj.length;i++)
    if(obj[i].title.includes("John Appleseed") && 
    /* obj[i].title.split("\"")[2].trim()=="<John Appleseed>" && */
    obj[i].tagName.toLowerCase()=="div"){
        divs[j++]=obj[i];
    }
console.log(divs);
-3

separate your div using div ID. Then get your respective div using that value of ID. Then in javascript you can use getElementByID.

-4

You can use Xpath,

.//div[contains(@class, 'Test')]

Then extract you required text from it.