JavaScript Add class to table in div containing <a> with specified href

I have a situation where I need to add a class to a table, but only if the table is inside a div with id 'mydiv' AND there is a row in table containing an <a target='_blank' href=...> element where the href contains the term 'myref', including the quotes. Is this easy in jQuery and if so, how do I do this? I just can't work out how to change an element while referring forward and backwards to others.

I tried the following, but it doesn't seem to work (I am relatively new to jQuery, as you can probably see from the below):

$('.mydiv').find('td').find('a[href]').contains('myref').parent.parent.addClass('myclass')

The following html snippet simulates my situation (I cannot change the html, just add some javascript and css):

<div id='mydiv'>
    <a target='_blank' href='blahblah'>Some text</a>
    <table>
        <tbody>
            <tr>
                <td><a target='_blank' href="some href">Some text</a></td>
                <td><a target='_blank' href="javascript:some javascript containing 'myref' in quotes">Some text</a></td>
            </tr>
        </tbody>
    </table>
    <table>
        <tbody>
            <tr>
                <td><a target='_blank' href="some href">Some text</a></td>
                <td><a target='_blank' href="javascript:some javascript without match">Some text</a></td>
            </tr>
        </tbody>
    </table>
</div>
<div id='otherdiv'>
    <table>
        <tbody>
            <tr>
                <td><a target='_blank' href="some href">Some text</a></td>
                <td><a target='_blank' href="javascript:some javascript containing 'myref' in quotes">Some text</a></td>
            </tr>
        </tbody>
    </table>
</div>

The first table needs to get the new class, the second and third don't.

Answer:1

Your searching for a class with the name mydiv - $(".mydiv") instead of searching for an ID

Change your code like this :)

$("#mydiv").find("td").find("a[href*='myref']").parents("table").addClass("myClass");
.myClass {
  background-color: lightblue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='mydiv'>
    <a target='_blank' href='blahblah'>Some text</a>
    <table>
        <tbody>
            <tr>
                <td><a target='_blank' href="some href">Some text</a></td>
                <td><a target='_blank' href="javascript:some javascript containing 'myref' in quotes">Some text</a></td>
            </tr>
        </tbody>
    </table>
    <table>
        <tbody>
            <tr>
                <td><a target='_blank' href="some href">Some text</a></td>
                <td><a target='_blank' href="javascript:some javascript without match">Some text</a></td>
            </tr>
        </tbody>
    </table>
</div>
<div id='otherdiv'>
    <table>
        <tbody>
            <tr>
                <td><a target='_blank' href="some href">Some text</a></td>
                <td><a target='_blank' href="javascript:some javascript containing 'myref' in quotes">Some text</a></td>
            </tr>
        </tbody>
    </table>
</div>
Answer:2

I want to exetend Tab component with es6 class like this: import React from "react"; import {Tab} from "material-ui"; class MyTab extends Tab { constructor(props){ super(props); } ...

I want to exetend Tab component with es6 class like this: import React from "react"; import {Tab} from "material-ui"; class MyTab extends Tab { constructor(props){ super(props); } ...

  1. extend material component
  2. angular extend material component
  3. material ui extend component
  4. extends angular material components

Is it possible in jquery to insert text after a span. For example, I would like to change the price value after the dollar sign below. <div id="price"> <span>$</span> 10.00 &...

Is it possible in jquery to insert text after a span. For example, I would like to change the price value after the dollar sign below. <div id="price"> <span>$</span> 10.00 &...

  1. changing text after effects
  2. changing text color after effects
  3. changing text colour after effects

I have a form with URL: localhost:83/mysite/item/UpdateItems/225 I have a temporary table in which I have loaded data and display in my form from temporary table. When I refresh the page I don't ...

I have a form with URL: localhost:83/mysite/item/UpdateItems/225 I have a temporary table in which I have loaded data and display in my form from temporary table. When I refresh the page I don't ...

  1. match string with regex
  2. match string with spaces regex
  3. match string with regex python
  4. match string with regex javascript
  5. match string with regex java
  6. match string with enum java
  7. match string with regex c#
  8. match string with enum c#
  9. match string with regex online
  10. match string with regex in js
  11. match string with regular expression in java
  12. match string with array javascript
  13. match string with pattern in java
  14. match string with regular expression in javascript
  15. match string with special characters regex
  16. match string with number regex
  17. match string with array php
  18. match string with regex ruby
  19. match string with regex php
  20. regex match string

Here is the object I want to re-create dynamically var myPath = { one: [ new google.maps.LatLng(1,2), new google.maps.LatLng(3,4)) ]}; Here is my try without success: var ...

Here is the object I want to re-create dynamically var myPath = { one: [ new google.maps.LatLng(1,2), new google.maps.LatLng(3,4)) ]}; Here is my try without success: var ...