0

I am trying to verify if the class of the "box" element contains the "current" class, but I am consistently receiving a false return.

<body>
  <script>
var message = $(".box").hasClass("current");
console.log(message);

  </script>
  <div class="container">
    <div class="box current"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
  <button class="button">Click Me!</button>
</body>

I am seeking to obtain a true value or the index of the element that evaluates as true for the "current" class when checking the class of the "box" elements.

  • In additional to the duplicate, none of your elements have the class name `box.current` (several have `box` and one has `current`) – Quentin Jan 17 '23 at 17:52
  • `$("box-container")` - This is looking for an element such as: `` `hasClass("box.current")` - This is looking for a class called `box.current`, not `current`. – David Jan 17 '23 at 17:52
  • @David code is edited – keremcelik Jan 17 '23 at 17:57
  • **always check your selectors at the time they run** - `console.log($(".box").length)` will output `0` in your code as your code runs before the element exists (in the edited code). Either move your code to just before `

    ` (where it belongs) or wrap it in doc.ready `$(() => { ..your code .. })`

    – freedomn-m Jan 17 '23 at 18:02

0 Answers0