I have a simple script (below) that on click (to the element with an id of #addClass) adds a class (.switcher) to the body tag. The script uses jquery.
I would like to know if it's possible to modify this script so that when the user moves on to the next page within the site the added class remains in place? In fact the class needs to stay in place until the element with an id #removeClass has been clicked no matter how many pages the user chooses to browse through. Please be gentle with your responses - I'm not much of a coder! If it's not possible with this script (or indeed with JS) does anyone have any suggestions as to how it may be possible

<script type="text/javascript">
 
    $("#addClass").click(function () {
	  $('body').addClass('switcher');
    });
    $("#removeClass").click(function () {
	  $('body').removeClass('switcher');
    });
</script>

Thanks

Daniel

Recommended Answers

All 5 Replies

Surfsup,

Are these pages hard-coded HTML or dynamically built with server-side scripting?

If the latter, then what?

Airshow

It's Wordpress site. The header file is sitewide so won't change throughout the site.
Does this help?

Mmm, the solution lies in saving the switcher false|true state somewhere. There are three basis choices; cookie, PHP $_session var, database. Each has its own scope.

Trouble is, WordPress has its own way of doing just about everything. I would suggest cookie but, due to cookie constraints, that may cut across WordPress's standard use of cookies. You really need a WordPress expert or to get lucky with a search in the WordPress documentation/forums.

(Before someone mentions it, a fourth option is to send the "switcher=0|1" on the url querystring merry-go-round but that would need to involve every internal url in the whole site so don't even think about it unless WP makes specific provision for such).

Sorry, it's not a gentle answer and sorry I can't help further.

Airshow

Thanks - it sound as though it may be possible - which is a great start! Would you mind posting an code example using of one of the methods you suggest and I'll give it a whirl?

Ok - so it sounds as though it is possible - however I have little/no chance of solving this without assistance. Can I get any help with this - pretty please... ?
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.