Sorry Mate, but u haven't explained ur problem clearly in the first post.
It doesn't really matter where u r using the script, it should work if its correct. I reckon that u haven't either added reference to JQuery file or added ur script properly in ur aspx file
Could you confirm me the following
- whether the script u have added is being rendered properly to client/browser?
- whether the jquery file u added is being rendered?
U could check these using developer tool (built-in) if u r using IE or using Firebug(plug-in) in case u r using Firefox.
mohramzan
Member
56 Points
23 Posts
Switch css file with button click through jquery
Feb 16, 2012 07:45 PM|LINK
<script type="text/javascript"> $(function () { var link = $('link'); $('button').on('click',function () { $this = $(this); var stylesheet = $this.data('file'); link.attr('href', stylesheet + '.css'); $this .siblings('button') .removeAttr('disabled') .end() .attr('disabled', 'disabled'); }); }); </script> <h1>My Website</h1> <br /> <button data-file="night">Night</button> <button data-file="day">Day</button>Ramesh T
Contributor
5131 Points
827 Posts
Re: Switch css file with button click through jquery
Feb 16, 2012 07:58 PM|LINK
Hello Mate,
You should to something like this
$("link[rel=stylesheet]").attr({href : name of ur stylesheet with in quotes ("day/night.css")});mohramzan
Member
56 Points
23 Posts
Re: Switch css file with button click through jquery
Feb 16, 2012 08:16 PM|LINK
what u mean ? can u explain a bit. my code works fine with html but not in aspx page or say it when i copy in visual studio it doesnt.
Ramesh T
Contributor
5131 Points
827 Posts
Re: Switch css file with button click through jquery
Feb 16, 2012 08:50 PM|LINK
Sorry Mate, but u haven't explained ur problem clearly in the first post.
It doesn't really matter where u r using the script, it should work if its correct. I reckon that u haven't either added reference to JQuery file or added ur script properly in ur aspx file
Could you confirm me the following
- whether the script u have added is being rendered properly to client/browser?
- whether the jquery file u added is being rendered?
U could check these using developer tool (built-in) if u r using IE or using Firebug(plug-in) in case u r using Firefox.
asteranup
All-Star
30184 Points
4906 Posts
Re: Switch css file with button click through jquery
Feb 17, 2012 05:42 AM|LINK
Hi,
You can try this-
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="<%="css/"+cssFileName +".css"%>" rel="stylesheet" type="text/css" /> <script type="text/javascript"> </script> </head> <body> <form id="form1" runat="server"> <h1>My Website</h1> <br /> <button value="night" name="cssFile" >Night</button> <button value="day" name="cssFile" >Day</button> </form> </body> </html>public string cssFileName = ""; protected void Page_Load(object sender, EventArgs e) { cssFileName = Request.Form["cssFile"] == null ? "Day" : Request.Form["cssFile"]; }Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
mohramzan
Member
56 Points
23 Posts
Re: Switch css file with button click through jquery
Feb 18, 2012 05:30 AM|LINK
the link you attached is the same question , and was asked me again. I am interested in jquery solution not other way as u proposed.
I got the solution from Mr Dave Ward @encosia.
basically i will have to add event.preventDefault(). to ovverdie on default behavior of form.
$(document).ready(function(){ $('button').click(evt){ evt.preventDefault(); }); });