Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Adding a class based on the first first of the string in jQuery

I am trying to add a class to a div based on the first word of the string. e.g

<div id="am-event-138 " class="am-event">
 <div class="am-event-title">Ballet (Live stream from School Ballet)</div>
</div>

<div id="am-event-139" class="am-event">
 <div class="am-event-title">Ballet</div>
</div>

<div id="am-event-140" class="am-event">
 <div class="am-event-title">Dance (Live stream from School Ballet)</div>
</div>

In the above, if the first word of the event title is Ballet, it should add the class ballet to am-event, if Dance then dance to am-event.

I am trying the following but the code does not seem to work as expected as it seems to take an occurrence of the word in the string rather than just the first word:

$(document).ready(function() {
  $(".am-event").each(function() {
    if (0 <= $(this).text().indexOf("Ballet")) {
      $(this).addClass("ballet");
    } else if (0 <= $(this).text().indexOf("Dance")) {
      $(this).addClass("dance");

    } else {}
  })
})
.ballet {
  background: red;
}

.dance {
  background: lightblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="am-event">
  <div id="am-event-138 " class="am-event">
    <div class="am-event-title">Ballet (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-139" class="am-event">
    <div class="am-event-title">Ballet
    </div>
  </div>

  <div id="am-event-140" class="am-event">
    <div class="am-event-title">Dance (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-141" class="am-event">
    <div class="am-event-title">Dance
    </div>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Trim the text so you can see if it startsWith either of those words - much more elegant (and semantically appropriate and understandable) than comparing against an indexOf result.

$(document).ready(function() {
  $(".am-event").each(function() {
    const text = $(this).text().trim();
    if (text.startsWith("Ballet")) {
      $(this).addClass("ballet");
    } else if (text.startsWith("Dance")) {
      $(this).addClass("dance");
    }
  })
})
.ballet {
  background: red;
}

.dance {
  background: lightblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="am-event">
  <div id="am-event-138 " class="am-event">
    <div class="am-event-title">Ballet (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-139" class="am-event">
    <div class="am-event-title">Ballet
    </div>
  </div>

  <div id="am-event-140" class="am-event">
    <div class="am-event-title">Dance (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-141" class="am-event">
    <div class="am-event-title">Dance
    </div>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza Report

0

Alternative, you can also use split(' ')[0] and compare text and based upon that add your class.

var b = "Ballet";
var d = "Dance"
$(".am-event-title").each(function() {
  var text = $(this).text().trim().split(' ')[0];
  if (text == b) {
    $(this).parent('div').addClass("ballet");
  } else if (text == d) {
    $(this).parent('div').addClass("dance");
  }
})
.ballet {
  background: red;
}

.dance {
  background: lightblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="am-event">
  <div id="am-event-138 " class="am-event">
    <div class="am-event-title">Ballet (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-139" class="am-event">
    <div class="am-event-title">Ballet
    </div>
  </div>

  <div id="am-event-140" class="am-event">
    <div class="am-event-title">Dance (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-141" class="am-event">
    <div class="am-event-title">Dance
    </div>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza Report

0

You need to trim off the whitespace. You can then use startsWith.

$(document).ready(function() {
  $(".am-event").each(function() {
    const text = $(this).text().trim();
    if (text.startsWith('Ballet')) {
      $(this).addClass('ballet');
    }
    if (text.startsWith('Dance')) {
      $(this).addClass('dance');
    }
  });
})
.ballet { background: red; }
.dance { background: lightblue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="am-event">
  <div id="am-event-138 " class="am-event">
    <div class="am-event-title">Ballet (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-139" class="am-event">
    <div class="am-event-title">Ballet
    </div>
  </div>

  <div id="am-event-140" class="am-event">
    <div class="am-event-title">Dance (Live stream from School Ballet)
    </div>
  </div>

  <div id="am-event-141" class="am-event">
    <div class="am-event-title">Dance
    </div>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!