0

I want to be able to trigger a function when a certain AJAX element is loaded. I'm using Brock Adam's excellent waitForKeyElements function to wait for a certain HTML element, which could appear via AJAX a long time after the main page is loaded. Normally I could use something like

waitForKeyElements( ".section-title", MyFunction );

to trigger MyFunction when the element with class section-title. Unfortunately, in this case there are multiple elements with class section-title; the one I'm looking for contains the text "Notes", something like this:

<div class="section-title"><h3>Notes</h3></div>

and it might or might not appear at all. I DON'T want to trigger if something else appears, like this:

<div class="section-title"><h3>Other Section Title</h3></div>

Is there any way to have waitForKeyElements trigger only when that specific text appears in that class of element? If waitForKeyElements is already triggered by the Other Section Title from the second example above, it will no longer be triggered by a later appearance of the correct Notes section from the first example above. I tried setting the bWaitOnce parameter to false, but waitForKeyElements still doesn't trigger for repeated appearances of elements satisfying the ".section-title" criterion. Thus, it would be futile to just use MyFunction to search for the Notes text.

Any help would be appreciated!

kwantum
  • 59
  • 6
  • @Brock Adams has been very helpful in answering my previous AJAX questions; I think I'm supposed to put his name after an at-sign to let him know about this question. Not sure if it would work if I had done it in the main text. Anyway, of course I'd be happy to hear from anyone else, too. – kwantum Jul 18 '19 at 17:15
  • 1
    Use jQuery `:contains()` selector, [examples](https://stackoverflow.com/a/7897178). – wOxxOm Jul 18 '19 at 17:35
  • Just to be clear: if I use `:contains()` , I would have to rewrite my own version of waitForKeyElements, correct? Or is there a way to pass it to the existing waitForKeyElements? I ask because I tried putting the waitForKeyElements code into my own code, to avoid having to depend on an external library, and it failed -- something about not being able to use the `$()` construct within TamperMonkey/GreaseMonkey. – kwantum Jul 18 '19 at 23:51
  • There's a warning inside waitForKeyElements that you should load jQuery e.g. `@require https://code.jquery.com/jquery-3.4.1.min.js` – wOxxOm Jul 19 '19 at 04:08

0 Answers0