4 Replies - 700 Views - Last Post: 08 July 2016 - 02:51 PM

#1 Lieoften   User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 357
  • Joined: 06-January 10

Keep added class/attribute when a div is refreshed

Posted 07 July 2016 - 09:49 PM

I'm trying to figure out how to keep this bit of code active once a div is refreshed:
function resource()
{
	$('#bresource').on('click', this, function(){ 
		$('#dres').removeAttr('class','hide'); 
		$('#bresource').on('click', this, function () { 
			$('#dres').addAttr('class','hide'); resource(); });
		});
	
}


I want #dres to remain visible/invisible depending on what the user had it set to before the rest of the code is enabled.

full code here:
function CMA(id)
{
		$('#starinfo').on('click','button',
			function(){
						var method = $(this).attr('id');
						$(this).prop("disabled", true).addClass('disabled '); 
							$.ajax({
									type:"POST",
									url: "cma.php",
									data: {uid:uid, sid:id, method:method, z:z},
									success: function(data) {
			
									if(data == 1)
									{
										$('#cMenu').empty().html("<img src='assets/ajax-loader.gif' id='loader'>");
										$('#cMenu').load('context_menu.php', function() { upgrade(); });
										$('#holder').empty();
										$('#holder').html("<img src='assets/ajax-loader.gif' id='loader'>");
													$('#holder').load('sinfor.php',{sid:id}, function() 
													{ 
														CMA(id);
													});
										$('#sinfor #exit a').on('click',function() {
											$('#sinfor').addClass('hidden');
											$('#sinfor #holder').empty();
											loadMap(x,y,z);
											 });
									}
									}
								});
							})	
}
function upgrade()
{
	bresource();
	$('#upgrade_holder').off('click', '.upgrade');
	$('#upgrade_holder').on('click', '.upgrade', 
		function() { 
		var id = $(this).attr('id');
			$.ajax({url:'upgrade.php', type:"POST", data:{'upgrade':id}, success: function(data) {
				if(data == 'true')
				{
					$('#cMenu').empty().html("<img src='assets/ajax-loader.gif' id='loader'>");
					$('#cMenu').load('context_menu.php', function() { upgrade(); });	
				}
				else
				{
					$('#error').dialog({
						height: 'auto',
						width: 300,
						modal: true,
						dialogClass: 'dlgfixed',
						position: "center",
						center: true
					});
				}
				 }
			});
			
			});
}
function bresource()
{
		resource();
	$('#bresource').off('click', 'button'); 
	$('#bresource').on('click', 'button',function(event) { 
		event.preventDefault();
		var type = $(this).attr('id'); 
		$.ajax(
			{
				url:'buyfuel.php', 
				type:"POST", 
				data:{fuel:type}, 
				dataType:"text",
				success: function(data) { 
				
				if(data == 'true')
				{
				$('#cMenu').load('context_menu.php', function() { upgrade(); });		
				}
				else
				{
				$('#error').dialog({
						height: 'auto',
						width: 300,
						modal: true,
						dialogClass: 'dlgfixed',
						position: "center",
						center: true
					});
				}
				}
		 } );	
	})
}
function loadMap(x, y,z)
{
	bresource();
	$('#map').load('map.php', {'x':x,'y':y,'z':z}, function() { 
	$('#map .star').on('click',
	function() 
		{
			
			var id = $(this).attr('id');
			$('#sinfor').removeClass('hidden').draggable();
			$('#holder').html("<img src='assets/ajax-loader.gif id='loader''>");
						$('#holder').load('sinfor.php',{sid:id}, function() 
						{ 
							CMA(id);
							upgrade();
						});
			$('#sinfor #exit a').on('click',function() { 
				$('#sinfor').addClass('hidden');
				$('#sinfor #holder').empty();
				 });
		})})
}
function resource()
{
	$('#bresource').on('click', this, function(){ 
		$('#dres').removeAttr('class','hide'); 
		$('#bresource').on('click', this, function () { 
			$('#dres').addAttr('class','hide'); resource(); });
		});
	
}
$(document).ready(function() {
	loadMap(x, y,z);
	if(z != 0)
	{
		$('#map').addClass('hell');	
	}
	upgrade();
});



Is This A Good Question/Topic? 0
  • +

Replies To: Keep added class/attribute when a div is refreshed

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Keep added class/attribute when a div is refreshed

Posted 08 July 2016 - 10:55 AM

Why would it change? Does it get removed and added again or something? If so, get the current state of it before replacing the element, and set the state back after adding it again.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Keep added class/attribute when a div is refreshed

Posted 08 July 2016 - 12:11 PM

Sounds like you're looking for the toggle function
$('#bresource').on('click', this, function () {
    $('#bresource').toggleClass('hide');
});


Was This Post Helpful? 0
  • +
  • -

#4 Lieoften   User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 357
  • Joined: 06-January 10

Re: Keep added class/attribute when a div is refreshed

Posted 08 July 2016 - 01:47 PM

View PostArtificialSoldier, on 08 July 2016 - 11:55 AM, said:

Why would it change? Does it get removed and added again or something? If so, get the current state of it before replacing the element, and set the state back after adding it again.


It only changes when the user refreshes the page (or when a jquery function reloads the page/div).

View PostCasiOo, on 08 July 2016 - 01:11 PM, said:

Sounds like you're looking for the toggle function
$('#bresource').on('click', this, function () {
    $('#bresource').toggleClass('hide');
});



I will give this a shot and reply back in a couple of minutes
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Keep added class/attribute when a div is refreshed

Posted 08 July 2016 - 02:51 PM

If you want to handle page refreshes then every time you change the class you need to store the state of the element in a cookie, and when the page loads you check for that cookie and set the element back to that state. If you have a function that removes and replaces the element then you don't need to use cookies for that, just save the state before you replace the element and restore it after.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1