Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>$(document).ready(function() { $(function() { var linkSet = $('#link1').add('#link2') linkSet.click(function() { linkSet.toggle(); }); }); }); </code></pre> <p>The add method allows you to add a different selector to the set of matchers, thus binding both clicks simultaneously. By saving the constructed set to a variable (<code>linkSet</code>), it stops you from having to traverse the DOM twice.</p> <p>The only two assumption made here, are</p> <p>1) That in the initial state only one is visible.</p> <p>2) That the id structure is meaningful, useful, and classes will not suffice.</p> <p><a href="http://jsfiddle.net/cuJBm/1/" rel="nofollow">http://jsfiddle.net/cuJBm/1/</a></p> <p>To answer your second question about setting an attribute on <code>#frame</code>. There are numerous ways of doing this. Perhaps the simplest is to add the following to your .click handler (after the <code>toggle</code>).</p> <pre><code>if ($(this).attr('id')=='link1'){ $('#frame').attr('src', 'www.google.com'); } else if ($(this).attr('id')=='link2'){ $('#frame').attr('src', 'www.yahoo.com'); } </code></pre> <p>Personally, I would probably add a custom attribute to your link elements, something like:</p> <pre><code>&lt;a id='link1' iframe-source='www.google.com'&gt; &lt;a id='link2' iframe-source='www.yahoo.com'&gt; </code></pre> <p>And then: (again, just after the <code>toggle</code>):</p> <pre><code>source = $(this).attr('iframe-source'); $('#frame').attr(src, source); </code></pre> <p>The reason for saving source if is that if you attempt to get <code>$(this)</code> within the <code>.attr</code> on <code>$('frame')</code>, it will (as always) return the currently matched element, ie <code>$('#frame')</code>.</p> <p>Alternately (and very similiarly to the above approach), you could use the innerHTML of the link. For example:</p> <pre><code>&lt;a id='link1'&gt;link1&lt;span style="display:none"&gt;www.google.com&lt;/span&gt;&lt;/a&gt; &lt;a id='link2'&gt;link2&lt;span style="display:none"&gt;www.yahoo.com&lt;/span&gt;&lt;/a&gt; </code></pre> <p>And then: (again, just after the toggle):</p> <pre><code>source = $(this).find('span').text(); $('#frame').attr(src, source); </code></pre> <p>Personally, I dislike this last method as it pollutes the DOM structure, leading to slightly more expensive rendering times, and (in my opinion) less readable code. Practically, all three methods work just fine.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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