1

Basically, if my first <td>within my <tr> has an <a> element inside it, I need to change my <tr>. That's as best as I could explain it right now. Example code is below. I thought about using document.links but I don't know if that's the way to go.

Before:

<tr>
    <td>11001122334</td> <!-- Number will be a link once the page loads due to a browser extension -->
    <td>Valid</td>
    <td>Still Valid</td>
</tr>

After:

 <tr class="valid">
    <td><a href="#">11001122334</td></a> <!-- Number will be a link once the page loads due to a browser extension -->
    <td>Valid</td>
    <td>Still Valid</td>
</tr>

2 Answers2

1
jQuery('tr td:first-of-type a').parent('tr').addClass('addedClass');

here's a fiddle: https://jsfiddle.net/pqgsmgo1/1/

explanation:

jQuery('tr td:first-of-type a')

selects any a elements that are descendant of the first td in a tr

.parent('tr')

selects the parent of the selected a element

.addClass('addedClass')

adds the "addedClass" class to the selected element(s)

DrCord
  • 3,917
  • 3
  • 34
  • 47
0

Check if the DOM is getting changed for a particular element using this solution, then use the selector that DrCord mentioned, but with .closest() function.

Demo:

var observeDOM = (function(){
    var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
        eventListenerSupported = window.addEventListener;

    return function(obj, callback){
        if( MutationObserver ){
            // define a new observer
            var obs = new MutationObserver(function(mutations, observer){
                if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
                    callback();
            });
            // have the observer observe foo for changes in children
            obs.observe( obj, { childList:true, subtree:true });
        }
        else if( eventListenerSupported ){
            obj.addEventListener('DOMNodeInserted', callback, false);
            obj.addEventListener('DOMNodeRemoved', callback, false);
        }
    }
})();

// Observe a specific DOM element:
observeDOM( document.getElementById('dom_element') ,function(){ 
    jQuery('tr td:first-child a').closest('tr').addClass('valid');
});
.valid{
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <tr>    
    <td>1234567899</td> <!-- Added this for test -->
    <td>11001122334</td> <!-- Number will be a link once the page loads due to a browser extension -->
    <td>Valid</td>
    <td>Still Valid</td>
  </tr>
  <tr>    
    <td><a href="#">11001122334</a></td> <!-- Added this for test -->
    <td>11001122334</td> <!-- Number will be a link once the page loads due to a browser extension -->
    <td>Valid</td>
    <td>Still Valid</td>
  </tr>
</table>
Community
  • 1
  • 1
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138