-2
S.fn.init [div.definitionAgelenidae, prevObject: S.fn.init(558)]

I have tried:

var Test02 = $(".definition").children().filter(function () {
    return $(this).css("visibility") == "visible";
};

console.log(Test02[0]);

I would like to get the string definitionAgelenidae

1 Answers1

0

Directly accessing $.fn.init is not a good idea, and should be avoided. Given the goal you describe in the comments (copied below) there is an alternative.

I would like to get the innerhtml of the visible element so I can have a more specific search result

In this case you can supply another condition in the function you provide to filter() which checks for the presence of a specific word. Something like this:

let term = 'dolor';

let $matches = $(".definition").children().filter(function() {
    return $(this).is(":visible") && $(this).text().toLowerCase().includes(term.toLowerCase());
}).addClass('match');
.match { color: #C00; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="definition">
  <p>Lorem ipsum dolor sit</p>  
</div>
<div class="definition">
  <p>Ipsum dolor sit amet</p>  
</div>
<div class="definition">
  <p>Foo bar fizz buzz</p>  
</div>
<div class="definition">
  <p>Dolor sit amet consectetur</p>  
</div>

Note that .toLowerCase() is used here to make the search case-insensitive. This can be removed if it's not required.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • can I set var term to multiple different words? Seeing there is more than one scenario at the end of the innerHTML of each of these elements. I feel like that may be my only option unless there was a way to filter S.fn.init again, but you saying it's a bad or dangerous thing to do. – Mathew Stratton Jan 06 '21 at 16:30
  • Sure, there's ways to search for multiple words within a string, [this](https://stackoverflow.com/a/44605049/519413) for example. I'd suggesting removing all thoughts about $.fn.init - it's not a solution to any issue :) – Rory McCrossan Jan 06 '21 at 16:36