Simple class change

I had 2 buttons with 2 different trigger classes before and these would fire other class changes when clicked…but now I’ve changed this to 1 button, so I’m trying to toggle classes on 1 button.

The code will work for 1 sequence - When the button is clicked, the assigned class events happen. But when the button is clicked a second time, the new class events don’t take place.

I know the code isn’t clean and right! The else statement was only 1 statement, but in a vein attempt of trying to get it to work, I extended the code to what you see below. Can’t see why this isn’t toggling classes =/

  <script>
	 $("#step1-trigger" ).click(function() {
	  if ("#step1-trigger" .ClassName == "trigger-a"){  
            ("#step1-trigger" .ClassName = "trigger-f");
	  }else{
	   	  if ("#step1-trigger" .ClassName == "trigger-f"){  
            ("#step1-trigger" .ClassName = "trigger-a")}}	   
 });
 </script>

hmm changed to switchClass and still the same. The function will change on first click, but not on second.

<script>
 $(function() {
    $( "#step1-trigger" ).click(function(){
      $( ".trigger-a" ).switchClass( "trigger-a", "trigger-f");
      $( ".trigger-f" ).switchClass( "trigger-f", "trigger-a");
    });
  });
  </script>

and…

<div id="step1-trigger" class="trigger-f"><button data-text-swap="Sell at an Auction" id="step1-select-button"  class="trigger-a ui-state-default ui-corner-all">Sell at a Fixed Price</button></div>

As there’s jquery UI classes on the button, I thought I’d wrap the button and apply the class changes to the div…should be fine right?

When this is clicked it should be firing the following (which was fine before)…both trigger-a / trigger-f have their own seperate rules like

jQuery(function( $ ){
  $( ".trigger-a" ).click(
    function( event ){
      event.preventDefault();
      document.getElementById('toggable-inline').className = 'show-inline';
      document.getElementById('quantity-c').className = 'hide';
    }
  );
});

Any obvious reason why this is only changing the class once?

The code you posted seems to be working.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.trigger-a {background:red}
.trigger-f {background:blue}
</style>
</head>

<body>
<div id="step1-trigger" class="trigger-f">
        <button data-text-swap="Sell at an Auction" id="step1-select-button"  class="trigger-a ui-state-default ui-corner-all">Sell at a Fixed Price</button>
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script> 
<script>    
     $(function() {
    $( "#step1-trigger" ).click(function(){
      $( ".trigger-a" ).switchClass( "trigger-a", "trigger-f");
      $( ".trigger-f" ).switchClass( "trigger-f", "trigger-a");
    });
  });
    
</script>    
</body>
</html>

Do you have a demo of the problem somewhere.

Must be a problem with the logic then. I removed trigger-a from class of button.
So container class changes on click, trigger-f rules below are triggered once, but trigger-a rules wont fire. I’ll reproduce the problem somewhere and send a link

thanks

jQuery(function( $ ){
  $( ".trigger-a" ).click(
    function( event ){
      event.preventDefault();
      document.getElementById('toggable-inline').className = 'show-inline';
      document.getElementById('quantity-c').className = 'hide';
    }
  );
});

I’m sure there’s an easier way to do this. I’m having problems recreating so it makes sense! Think it depends on when the class changes and when the click is registered…

ok uploaded here…
http://cwilliams.comule.com/trigger/

selector events should trigger when class changes…likewise for selector1

I need… either button or div container to change class to trigger-a or trigger-f, so when it is clicked it will fire the events in selector/selector1

Thanks for looking

probably registering the class change, but theres then no click on the trigger to fire the selector events…What you think?

You don’t seem to be linking to the jquery.ui correctly in your demo.

It’s here:

<script type="text/javascript" src="http://cwilliams.comule.com/trigger/jquery-ui.min.js"></script>

You have it placed in a non existent js folder.

ye sorry…quickly copied and pasted from another file. Dont think that wouldve made a difference to the function anyway…
correctly linked now…

I think the prob is the class name is changing, but there’s no leading click to trigger selector js

HI,

Because you are adding classes dynamically you need to target them with .on ( as they don’t exist until they are added.

You also had specificity issues as an id of display:none will win out against a class of display:none.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
#step1-select-button {
    padding: .5em 1em;
    text-decoration: none;
    background:#259DE0;
    color:#fff;
    cursor:pointer;
    margin-left:54px;
    display:inline-block;
}
.show {
    display:block
}
.hide {
    display:none
}
.trigger-a {
    background:red
}
.trigger-f {
    background:blue
}
</style>
<link rel="stylesheet" href="http://cwilliams.comule.com/trigger/jquery-ui.css">
</head>

<body>
<p class="step1-state-txt">Change to... </p>
<div id="step1-trigger" class="trigger-a">
        <button data-text-swap="Sell at an Auction" id="step1-select-button"  class="ui-state-default ui-corner-all">Sell at a Fixed Price</button>
</div>
</div>
<!--end change current c-->

<div id="test-a"> trigger a active </div>
<div id="test-f" class="hide"> trigger f active </div>
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script> 
<script type="text/javascript" src="http://cwilliams.comule.com/trigger/jquery-ui.min.js"></script> 
<script>

jQuery(function( $ ){
            $( "body" ).on ("click",'.trigger-a',function( event ){
       event.preventDefault();
    
            document.getElementById('test-a').className = 'hide';
            document.getElementById('test-f').className = 'show';

             });
        });
    
/*clear form state*  for now option-buynow-c and option-reserve-c are hidden if auction is clicked....
need to remove checked from box and clear form values      
/*remove checkbox // id: cb-reserve // id: cb-buynow // http://elmahdim.com/checkbo/ */
</script><!--need to combine--> 
<script type="text/javascript" src="http://cwilliams.comule.com/trigger/selector1.js"></script><!--...with this--> 
<script>
jQuery(function( $ ){
            $( "body" ).on ("click",'.trigger-f',function( event ){
      event.preventDefault();                    
            document.getElementById('test-a').className = 'show';
            document.getElementById('test-f').className = 'hide';
            
             }
           );
        });

</script> 
<script>
 $(function() {
    $( "#step1-trigger" ).click(function(){
      $( ".trigger-a" ).switchClass( "trigger-a", "trigger-f");
      $( ".trigger-f" ).switchClass( "trigger-f", "trigger-a");
    });
  });
  </script> 
<script>        
$("#step1-select-button").on("click", function() {
  var el = $(this);
  if (el.text() == el.data("text-swap")) {
    el.text(el.data("Sell at a Fixed Price"));
  } else {
    el.data("Sell at a Fixed Price", el.text());
    el.text(el.data("text-swap"));
  }
});
</script>
</body>
</html>

If you can explain what you need to happen I’m sure this can all be tidied up.

Ahh I see. I’ll try to clearly explain the aim here then…

Before I had two buttons. 1 with class trigger-a and 1 with class trigger-f. On click they would execute the js class changes in selector and selector1 (can see these on server)

But now, the 2 buttons have been changed into 1 button. The 1 button label will change via data-text-swap…this is fine. But now I need to be able to assign a class for each label change, so the button triggers the correct selector.

If label is auction, class trigger-a should be active and on click the selector js should fire which changes/hides some fixed sale form properties. And in reverse, if button label is fixed sale, it should be assigned the trigger-f class which on click changes/hides some auction form properties

suppose it would be ideal to apply the class in the text-swap…html & js for this is below. I avoided trying to make changes to the button class, as it already has 2 jq-ui classes.

<button data-text-swap="Sell at an Auction" id="step1-select-button"  class="ui-state-default ui-corner-all">Sell at a Fixed Price</button>

<script>		
$("#step1-select-button").on("click", function() {
  var el = $(this);
  if (el.text() == el.data("text-swap")) {
    el.text(el.data("Sell at a Fixed Price"));
  } else {
    el.data("Sell at a Fixed Price", el.text());
    el.text(el.data("text-swap"));
  }
});
</script>

Wouldn’t it be simpler just to do something like this.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.auction{display:none}

</style>
</head>

<body>
<button data-text-swap="Sell at an Auction" id="step1-select-button"  class="ui-state-default ui-corner-all">Sell at a Fixed Price</button>
<div class="sale">Sale form properties</div>
<div class="sale">Sale form properties</div>
<div class="sale">Sale form properties</div>
<div class="sale">Sale form properties</div>
<div class="sale">Sale form properties</div>

<div class="auction">Auction form properties</div>
<div class="auction">Auction form properties</div>
<div class="auction">Auction form properties</div>
<div class="auction">Auction form properties</div>
<div class="auction">Auction form properties</div>
<div class="auction">Auction form properties</div>



<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script> 
<script>        
$("#step1-select-button").on("click", function() {
  var el = $(this);
  if (el.text() == el.data("text-swap")) {
    el.text(el.data("Sell at a Fixed Price"));
        $('.sale').show();
        $('.auction').hide();
  } else {
    el.data("Sell at a Fixed Price", el.text());
    el.text(el.data("text-swap"));
        $('.sale').hide();
        $('.auction').show();
  }
});
</script>



</body>
</html>

If this was the initial plan, probably yes. But now there’s about 10-15 hide/show rules for both trigger selections. For example an auction form may use some fixed sale labels/input… and vice versa.

maybe if selectors said alogn the lines of
if step1-select-button label value == “blabla”{
document get element by id…change class.

then there’s no class change of button needed…just a label check

what you think?

I don’t think you need to get that complicated as you are only showing and hiding elements based on one of two states. Just add the classes to the ones that need hiding or showing and leave any common ones without classes so that they remain visible for both.

Alternatively just group all the sale inputs in one div and all the auction inputs in another div and then just hide and show each div as required.

I think you may need to give a more complex example if the above criteria doesn’t work or meet your requirements. It seems you only have two states to work with so I can’t see how it needs to be more complex.:slight_smile:

Much much better, less code, nice clean and simple. You’re a JS superstar PaulOB!

Thank you ever so much =)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.