HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Please upgrade here. These earlier versions are no longer being updated and have security issues.

Adding a stylesheet switcher - but how to add an ID to the stylesheet url?

edited October 2011 in Vanilla 2.0 - 2.8
I need to add a stylesheet switcher on my forums. I have a script using jQuery but it requires that the stylesheet I wish to replace have an ID I can hook into.

e.g.

<link id="styletoswitch" rel="stylesheet" type="text/css" href="/minimal/themes/Minimal/design/custom.css?v=1.0" media="screen" />

So how can I add an ID parameter to the custom.css stylesheet link?

I don't mind if it's a hack as it's just the one domain.

Best Answer

  • x00x00 MVP
    Accepted Answer
    normally a jquery extension you can use any selector it doesn't have to be id

    so you could use $('link[href*="/design/custom.css"]') or add an id dynamically $('link[href*="/design/custom.css"]').attr('id','styletoswitch')

    you can also do it in php but why bother?

    grep is your friend.

Answers

  • x00x00 MVP
    Accepted Answer
    normally a jquery extension you can use any selector it doesn't have to be id

    so you could use $('link[href*="/design/custom.css"]') or add an id dynamically $('link[href*="/design/custom.css"]').attr('id','styletoswitch')

    you can also do it in php but why bother?

    grep is your friend.

  • Ah yeah! Is there a way to target say the second style link on a page?

    As the href for that changes depending on the style that is set.
  • x00x00 MVP
    edited October 2011
    Yes of course

    $('link:eq(1)')

    is this going to be reliable though? I would use something more specific.

    If the style changes but there is a general pattern such as a prefix then you could use that.

    http://api.jquery.com/category/selectors/

    grep is your friend.

  • edited October 2011
    Thanks x00. Your first example worked when i realized I could make it just

    $('link[href*="custom"]')
  • you got the idea.

    grep is your friend.

Sign In or Register to comment.