3 Replies - 980 Views - Last Post: 27 May 2011 - 06:09 AM

#1 evinrows   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 141
  • Joined: 03-August 09

Saving results of checkbox / radiobox.

Posted 26 May 2011 - 08:03 AM

So I'm writing something for my work and I'm making a customization area for machinery. Right now, I'm using JQuery to save the boxes that are checked whenever you leave the current selection so that when you come back to it, they're still checked like so:

$(document).ready(function() {
	$(".navbutton").click(function() {
		if ($(".1").hasClass("chosenbutton")) {
			op1 = $("#suboptions").html();
		}
		if ($(".2").hasClass("chosenbutton")) {
			op2 = $("#suboptions").html();
		}
		if ($(".3").hasClass("chosenbutton")) {
			op3 = $("#suboptions").html();
		}
		if ($(".4").hasClass("chosenbutton")) {
			op4 = $("#suboptions").html();
		}
		if ($(".5").hasClass("chosenbutton")) {
			op5 = $("#suboptions").html();
		}
		if ($(".6").hasClass("chosenbutton")) {
			op6 = $("#suboptions").html();
		}
		$(".navbutton").removeClass("chosenbutton");
		$(this).addClass("chosenbutton");
	});
	$(".1").click(function() {
		$("#suboptions").html(op1);
	});
	$(".2").click(function() {
		$("#suboptions").html(op2);
	});
	$(".3").click(function() {
		$("#suboptions").html(op3);
	});
	$(".4").click(function() {
		$("#suboptions").html(op4);
	});
	$(".5").click(function() {
		$("#suboptions").html(op5);
	});
	$(".6").click(function() {
		$("#suboptions").html(op6);
	});          
});          



This works for I.E. but not firefox because firefox doesn't save the results of checkboxes in HTML, it stores it in memory somewhere. Is there any way I can do this without using more variables? Also, is there any way I can do this without using the variables I've already used, because they make the code look ugly. Thanks, guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Saving results of checkbox / radiobox.

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Saving results of checkbox / radiobox.

Posted 26 May 2011 - 08:43 AM

Once you change the page all Javascript variables and states are gone. If you want to keep the state of the check boxes you must use PHP to store them in a SESSION.

You can however use the :checked attribute to know if checkbox is checked or not
if($(".classname")[0].checked == true)
	//it's checked
else
	//it's not checked

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Saving results of checkbox / radiobox.

Posted 26 May 2011 - 11:11 AM

or somewhat simpler: $(".classname:checked").each( … );
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Saving results of checkbox / radiobox.

Posted 27 May 2011 - 06:09 AM

Moved to jQuery
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1