javascript - Automatically add a certain class, whenever <Form> code contains a certain value? -
i want add special class each form contains word special
part of action url.
but have many forms, , can't think of automated way this.
the way came create following code, have create such code each , every form, using different index number:
<?php $case1 = "special"; $case2 = "none"; if($case1 == "none") { $class1 = ""; // don't add class } else { $class1 = "effect"; // add `effect` class } if($case2 == "none") { $class2 = ""; // same goes here } else { $class2 = "effect"; // , here } ?>
html:
<form action="/go/<?= $case1 ?>" method="post" target="_blank"> <input type="submit" class="<?= $class1 ?> general-class" value="submit"> </form> <form action="/go/<?= $case2 ?>" method="post" target="_blank"> <input type="submit" class="<?= $class2 ?> general-class" value="submit"> </form>
output:
<form action="/go/special" method="post" target="_blank"> <input type="submit" class="effect general-class" value="submit"> </form> <form action="/go/none" method="post" target="_blank"> <input type="submit" class="general-class" value="submit"> </form>
is there automated way this?
basically have 2 types of forms, 1 should opened using jquery
plugin (henace special class), , other should open in normal way.
my way differentiate between them insert $case[i]
variable action url, have either way.
perhaps there's complete different way achieve this, don't know.
edit:
real form generated manually, code:
<form action="/go/<?= $item ?>/<?php echo $case1 ; ?>" method="post" target="_blank"> <input name="a" type="hidden" value="<?php echo $a; ?>"/> <input name="b" type="hidden" value="<?php echo $b; ?>"/> <input name="c" type="hidden" value="<?php echo $c; ?>"/> <input name="d" type="hidden" value="<?php echo $d; ?>"/> <input type="submit" class="<?= $class1 ?> general-class" value="click me"></form>
(all variables being given values @ start of php block see above)
there's 2 ways - php , jquery. based on code, don't have enough info know if can use php way or not, here's jquery way:
// wait until document ready jquery(function($) { // find forms have "special" in action $('form[action*="special"]').each( function() { // within form, find submit button, , add "effect" class $(this).find('input[type="submit"]').addclass('effect'); } ); });
and, shorter / less verbose way:
jquery(function($) { // find forms have "special" in action, find input, , add class $('form[action*="special"] input[type="submit"]').addclass('effect'); });
the php way
so, in php, recommend writing simple function, calling it.
function get_class( $slug ) { $class_map = array( 'special' => 'effect', 'none' => '', // .. add others here if appropriate return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : ''; );
then use in php so:
<form action="/go/<?= $item ?>/<?php echo $case1 ; ?>" method="post" target="_blank"> <input name="a" type="hidden" value="<?php echo $a; ?>"/> <input name="b" type="hidden" value="<?php echo $b; ?>"/> <input name="c" type="hidden" value="<?php echo $c; ?>"/> <input name="d" type="hidden" value="<?php echo $d; ?>"/> <input type="submit" class="<?= get_class( $case1 ); ?> general-class" value="click me"></form>
while may not seem big value, if started applying concepts code, see value start adding up.
Comments
Post a Comment