Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an implementation. It borrows much of the event handler logic from <a href="https://stackoverflow.com/questions/857439/internet-explorer-and-javascript-event-currenttarget">this post</a>. YOU SHOULD NOT USE THIS CODE IN PRODUCTION. There is nothing worse than copying and pasting code you do not understand. Read and practice, when you fully understand this code you will be able to write a much better implementation that you will be able to maintain and support.</p> <pre><code>&lt;link rel="stylesheet" type="text/css" href="style1.css" id="switch_style"/&gt; </code></pre> <p>JS stuff:</p> <pre><code>&lt;script type="text/javascript"&gt; (function(){ function getEvent (e) { if(!e) e = window.event; if(e.srcElement) e.target = e.srcElement; return e; } function addEvent (object,type,listener,param) { if(object.addEventListener) { object.addEventListener(type, function(e){ listener.call(object, e, param); }, false ); } else if(object.attachEvent) { object.attachEvent('on'+type, function(e){ e = getEvent(e); listener.call(object, e, param); } ); } } var default_style = 1, container = document.getElementById('switch_container'), controls = container.childNodes, styles = document.getElementById('switch_style'); function switch_style(event){ event = getEvent(event); s = event.target.getAttribute('value'); styles.setAttribute('href, 'style' + s + '.css'); } [].map.call(controls, function( e, i, l ){ if( e.nodeType === 1 &amp;&amp; e.getAttribute('type') === 'radio' ) { addEvent( e, 'click', switch_style ); } }) // bind the function to the event handler. })() &lt;/script&gt; </code></pre> <p>DOM stuff:</p> <pre><code>&lt;div id="switch_container"&gt; &lt;input type="radio" name="switch" value="1"/&gt;style 1&lt;br/&gt; &lt;input type="radio" name="switch" value="2"/&gt;style 2&lt;br/&gt; &lt;input type="radio" name="switch" value="3"/&gt;style 3&lt;br/&gt; &lt;input type="radio" name="switch" value="4"/&gt;style 4&lt;br/&gt; &lt;input type="radio" name="switch" value="5"/&gt;style 5&lt;br/&gt; &lt;input type="radio" name="switch" value="6"/&gt;style 6&lt;br/&gt; &lt;/div&gt; </code></pre> <p><a href="http://jsfiddle.net/zj7Bm/2/" rel="nofollow noreferrer">JSFIDDLE</a></p>
 

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