Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you execute a JavaScript function in HTML the way you're doing, that function has no awareness of what HTML element contains it. One way to bypass that problem (my favorite way) is to put those variables in <code>data-</code> attributes for the DOM element, and use jQuery to extract and operate on them using <code>.data()</code>.</p> <p>Your second problem is that the <code>h2</code> tag isn't a parent (container) of the <code>span.team</code> element. A little more DOM traversal is necessary to get to the one from the other.</p> <p><a href="http://jsfiddle.net/mblase75/fzwBM/" rel="nofollow">http://jsfiddle.net/mblase75/fzwBM/</a></p> <p>new HTML:</p> <pre><code>&lt;div class="tournaments"&gt; &lt;h2 class="trigger"&gt;Tournament 1&lt;span class="status"&gt;&lt;/span&gt;&lt;/h2&gt; &lt;div class="toggle_container"&gt; &lt;div class="block"&gt; &lt;h3&gt;Details&lt;/h3&gt; &lt;p&gt;Deadline: DATE&lt;/p&gt; &lt;p&gt;Teams: &lt;span class="team" data-a="3" data-b="7"&gt;&lt;/span&gt; &lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;h2 class="trigger"&gt;Tournament 2 &lt;span class="status"&gt;&lt;/span&gt;&lt;/h2&gt; &lt;div class="toggle_container"&gt; &lt;div class="block"&gt; &lt;h3&gt;Details&lt;/h3&gt; &lt;p&gt;Deadline: DATE&lt;/p&gt; &lt;p&gt;Teams: &lt;span class="team" data-a="3" data-b="7"&gt;&lt;/span&gt; &lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>new JS:</p> <pre><code>$(document).ready(function() { $(".toggle_container").hide(); $("h2.trigger").click(function() { $(this).toggleClass("active").next().slideToggle("slow"); }); $('span.team').each(function() { var a = $(this).data('a'); var b = $(this).data('b'); var $status = $(this).closest('.toggle_container').prev('.trigger').find('.status'); console.log($status.length); if (a != b) { // if a/b!=1, then a!=b $status.text("OPEN").css("color", "green"); } else { $status.text("CLOSED").css("color", "red"); } $(this).text(a + " / " + b); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload