jQuery toggleClass example

Toggle class name on click in jQuery

by fschwiet

HTML

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:lang="es" id="2d98a6aeade943e0a773f4447f0c8477-0" class="displacy" width="4100" height="587.0" direction="ltr" style="max-width: none; height: 587.0px; color: #000000; background: #ffffff; font-family: Arial; direction: ltr">
<text class="displacy-token" fill="currentColor" text-anchor="middle" y="497.0">
    <tspan class="displacy-word" fill="currentColor" x="50">Y</tspan>
    <tspan class="displacy-lemma" dy="2em" fill="currentColor" x="50">Y</tspan>
    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="50">CCONJ</tspan>
</text>

<text class="displacy-token" fill="currentColor" text-anchor="middle" y="497.0">
    <tspan class="displacy-word" fill="currentColor" x="200">para</tspan>
    <tspan class="displacy-lemma" dy="2em" fill="currentColor" x="200">parir</tspan>
    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="200">ADP__AdpType=Prep</tspan>
</text>

<text class="displacy-token" fill="currentColor" text-anchor="middle" y="497.0">
    <tspan class="displacy-word" fill="currentColor" x="350">colmo</tspan>
    <tspan class="displacy-lemma" dy="2em" fill="currentColor" x="350">colmar</tspan>
    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="350">NOUN</tspan>
</text>

<text class="displacy-token" fill="currentColor" text-anchor="middle" y="497.0">
    <tspan class="displacy-word" fill="currentColor" x="500">ese</tspan>
    <tspan class="displacy-lemma" dy="2em" fill="currentColor" x="500">ese</tspan>
    <tspan class="displacy-tag" dy="2em" fill="currentColor" x="500">DET__Gender=Masc|Number=Sing|PronType=Dem</tspan>
</text>

<text class="displacy-token" fill="currentColor" text-anchor="middle" y="497.0">
    <tspan class="displacy-word" fill="currentColor" x="650">proceso</tspan>
    <tspan class="displacy-lemma" dy="2em" fill="currentColor" x="650">procesar</tspan>
    <tspan class="displacy-tag" dy="2em" fill="currentColor"...

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}

JavaScript

// find elements
var banner = $("#banner-message")
var button = $("button")

// handle click and add class
button.on("click", () => {
  banner.toggleClass("alt")
})

var tags = $(".displacy-tag").each(t => t.text);

console.log(tags);