Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery - ID not defined. Using a dynamic ID in a function?
    text
    copied!<p>I'm having a bit of trouble getting a script to work within a CMS. It's a relatively simple jQuery show/hide that was working fine until I needed to use it multiple times on one page. I tried to convert it to use each DIV's individual ID, but now it returns an error in Firebug saying</p> <blockquote> <p>id not defined.</p> </blockquote> <p>jQuery</p> <pre><code>$('.articleSlide').each(function () { var current = $(this); current.attr("box_h", current.height()); $(".articleSlide").css("height", "250px"); $(".showHide").html('&lt;a href="#"&gt;More&lt;/a&gt;'); $(".showHide a").attr("href", "javascript:void(0)"); $(".showHide a").click(function() { openSlider() }) }); function openSlider() { var open_height = $("#articleSlide_" + id).attr("box_h") + "px"; $("#articleSlide_" + id).animate({"height": open_height}, {duration: "slow" }); $(".showHide").html('&lt;a href="#"&gt;More&lt;/a&gt;'); $(".showHide a").click(function() { closeSlider() }) } function closeSlider() { $("#articleSlide_" + id).animate({"height": "250px"}, {duration: "slow" }); $(".showHide").html('&lt;a href="#"&gt;More&lt;/a&gt;'); $(".showHide a").click(function() { openSlider() }) } </code></pre> <p>HTML</p> <pre><code>&lt;div class="articleSlide" id="articleSlide_1"&gt; &lt;p&gt;We work closely with clients to provide effective solutions for a wide variety of products and applications through brand creation and management, video and motion graphics, marketing and advertising, digital and editorial.&lt;/p&gt; &lt;p&gt;We understand markets and how to communicate in a multi-platform environment.We work closely with clients to provide effective solutions for a wide variety of products and applications through brand creation and management, video and motion graphics, marketing and advertising, digital and editorial.&lt;/p&gt; &lt;/div&gt; &lt;div class="showHide"&gt; </code></pre> <p><strong>EDIT</strong></p> <p>OK I think I misunderstood the ID bit I included. I basically wanted it to be able to pick up the individual ID of each show/hide div so it only opened that one. This is my current script that works, but if you have more than one div on the page it opens and closes them all</p> <pre><code>$(".articleSlide").each(function () { var current = $(this); current.attr("box_h", current.height()); $(".articleSlide").css("height", "250px"); $(".showHide").html('&lt;a href="#"&gt;More&lt;/a&gt;'); $(".showHide a").attr("href", "javascript:void(0)"); $(".showHide a").click(function() { openSlider() }) }); function openSlider() { var open_height = $(".articleSlide").attr("box_h") + "px"; $(".articleSlide").animate({"height": open_height}, {duration: "slow" }); $(".showHide").html('&lt;a href="#"&gt;More&lt;/a&gt;'); $(".showHide a").click(function() { closeSlider() }) } function closeSlider() { $(".articleSlide").animate({"height": "250px"}, {duration: "slow" }); $(".showHide").html('&lt;a href="#"&gt;More&lt;/a&gt;'); $(".showHide a").click(function() { openSlider() }) } &lt;a href="#"&gt;More&lt;/a&gt; &lt;/div&gt; </code></pre>
 

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