0

I'm trying to make a quick CSS style switcher from a dropdown in HTML. The JS is external, and called at the end of the head right before the form.

My JS:

function ChangeTheTheme(){
    txt = document.getElementsByTagName("link");
    switch(document.mytheme.themes.selectedIndex) {
    case 0:
        alert("Choice 0");
        txt[0].href = "style.css";
        break;
    case 1:
        alert("Choice 1");
        txt[0].href = "style2.css";
        break;
    }
}

HTML line that matters is :

<form name="mytheme">
<select name=”themes” onChange=”ChangeTheTheme();”>

I tried adding alert("working"); in the beginning of the function. It's showed me that the function isn't being called at all.

The dropdown should switch CSS styles to whatever's currently selected. No buttons to submit or anything. That's what I understand onchange should do.

1
  • Also am I right to say that in JS if one thing goes wrong, everything goes wrong? The HTML will just stop right there? Why was that edited out of my question? May 10, 2012 at 20:09

1 Answer 1

7

You had fancy quotes instead of normal quotes. (this often happens when copying from Wordpress-powered blogs, be careful when you do so! I copied a PHP code from a Wordpress blog once, it crashed my blog for a while until I could figure out what was going wrong...)

<select name="themes" onChange="ChangeTheTheme();">
3
  • I did copy paste a lot of this from a tutorial book...thanks that's probably it, that's twice you've saved me I think :P May 10, 2012 at 20:11
  • Thanks, this was the problem. Good job! May 10, 2012 at 20:12
  • Heh, no problem, glad I could help! May 10, 2012 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.