0

I am trying to obtain only the birth date (not the word Birthday but the date) from the DOM.

I've tried creating a regex to replace everything before Birthday</th><td> with a '' but I can't figure out how to remove the rest after the date as well as a split & join. Before resorting to cross referring an array of EVERY day in the calendar year, I thought I'd turn to StackOverflow.

I can get the HTML using:

document.getElementById('personal-info-view').innerHTML

which returns:

"<tbody><tr><th scope="row">Birthday</th><td>October 21</td></tr><tr><th scope="row">Marital Status</th><td>Single</td></tr></tbody>"

From here:

<table id="personal-info-view" class="additional-info-listing" summary="Personal Details">
   <tbody>
     <tr>
        <th scope="row">Birthday</th> 
        <td>October 21</td>
     </tr>
     <tr>
        <th scope="row">Marital Status</th>
        <td>Single</td>
     </tr>
  </tbody>
</table>

CONTEXT UPDATE:

I am trying to get an alert when my connections on LinkedIn have upcoming birthday's while looking though their profiles.

Community
  • 1
  • 1
anonameuser22
  • 108
  • 12

5 Answers5

2
  1. Using jQuery:

    $(function(){
        var birthdate = $("tbody > :first-child > :last-child").text();
        alert(birthdate);
    });
    
  2. Using Javascript:

    $(function(){
        var birthdate = document.querySelector("tbody > :first-child > :last-child").innerHTML;
        alert(birthdate);
    });
    
  3. Regex: (see @Washington Guedes, glad to give credit to other answerers).

Hope this helps!

Advice: Aiming towards the future you should consider the selector of your choice and storing it in a variable like so:

var birthdaySelector = "tbody > :first-child > :last-child";

If the site you are working on does change for whatever reason, or you find an optimal selector on your own, you can easily change it by modifying the value in the birthdaySelector variable. That way you can also implement it in whichever JS way you like.

If you perhaps are wondering how to use it, you can do it like so:

jQuery

var birthdate = $(birthdaySelector).text();

Javascript

var birthdate = document.querySelector(birthdaySelector).innerHTML;
AGE
  • 3,752
  • 3
  • 38
  • 60
1

Try this regex:

[^>]+\d+(?=<)

Regex live here.

It would be:

document.getElementById('personal-info-view').innerHTML.match(/[^>]+\d+(?=<)/)[0];

Hope it helps.

1

You could just get the DOM element directly with .querySelectorAll.

document.querySelectorAll("#personal-info-view tbody tr td")[0].innerHTML;
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
1

I think I'd try this:

document.getElementById('personal-info-view').children[0].children[0].children[1].innerHTML;

Demo

Would be better if you could get an ID on that table cell, though. This is pretty fragile (DOM-dependent).

isherwood
  • 58,414
  • 16
  • 114
  • 157
0

Try something like this:

~\w+ \d+~
frosty
  • 2,559
  • 8
  • 37
  • 73