March 16, 201016 yr Hey everyone I have the following jquery code that hides or shows a div depending on which button is pressed. $(document).ready(function(){ $("input#1").click(function(event){ event.preventDefault(); $("div#footer3").css('display', 'none'); $("div#footer1").css('display', 'block'); }); $("input#2").click(function(event){ event.preventDefault(); $("div#footer3").css('display', 'block'); $("div#footer1").css('display', 'none'); }); }); It all works fine except I'd like to improve it a bit. When you click "input#2" more than once it cycles through the divs but I just want it to show footer3 and then if it's pressed again nothing will happen. Also when you click "input#1" after clicking "input#2" it takes you to the top of the page where as I want it to stay where the div is located. I hope this all makes sense. Here is the link to the page - http://www.dpmgroup.co.uk/preview/aspire/ The buttons I'm talking about are located at the bottom of the page. Thanks for any help.
March 17, 201016 yr This should help with the first part - this could be cleaned up by using another function, but it should work (I've not tested it) $(document).ready(function(){ // set default active div var current_div = 1; $("input#1").click(function(event){ // if div 1 is not visible change state if(current_div != 1){ event.preventDefault(); $("div#footer3").css('display', 'none'); $("div#footer1").css('display', 'block'); current_div = 1; } }); $("input#2").click(function(event){ // if div 3 is not visible change state if(current_div != 3){ event.preventDefault(); $("div#footer3").css('display', 'block'); $("div#footer1").css('display', 'none'); current_div = 3; } }); });
Create an account or sign in to comment