jeudi 28 juillet 2016

Toggle Checkbox if element has class

I ahve an list with elements, which get the class 'added' if clicked. This element shall be transferred to the form. Therefore I have made a list of checkboxes, which have the same classes as the 'li' from the list.

I wrote a jQuery script, which shall check, if the 'li' is clicked (hasClass) and then the proper checkbox should have the attribute 'checked'. But it is not working properly. Don't know what I have made wrong...

HTML:

<div class="container">
                    <div class="row row-centered konf-wrapper-1">
                        <div class="col-md-4 label-wrap">
                            <h1 class="header-label">Konzeption</h1>
                            <ul class="li-wrap">
                                <li class="added haken">Kommunikationsstrategie</li>
                                <li class="0 hide-me add-btn point">Zielgruppendefinition</li>
                            </ul>
                        </div>

                        <div class="col-md-4 label-wrap">
                            <h1 class="header-label">Kommunikationsmaßnahmen</h1>
                            <ul class="li-wrap">
                                <li class="1 hide-me add-btn point">Namensfindung & Logoentwicklung</li>
                                <li class="2 hide-me add-btn point">Exposé</li>
                                <li class="3 hide-me add-btn point">Textdesign</li>
                                <li class="4 hide-me add-btn point">Anzeigenkampagnen</li>
                                <li class="5 hide-me add-btn point">Außenwerbung</li>
                                <li class="6 hide-me add-btn point">Bautafelgestaltung</li>
                            </ul>
                        </div>
                    </div>

                    <div class="row row-centered konf-wrapper-2 label-wrap">
                        <div class="col-md-4 label-wrap">
                            <h1 class="header-label">Online-Marketing</h1>
                            <ul class="li-wrap">
                                <li class="8 hide-me add-btn point">Website</li>
                                <li class="9 hide-me add-btn point">Social Media</li>
                                <li class="10 hide-me add-btn point">Digitale Präsentation</li>
                            </ul>
                        </div>

                        <div class="col-md-4 label-wrap">
                            <h1 class="header-label">Virtuelle Welten</h1>
                            <ul class="li-wrap">
                                <li class="11 hide-me add-btn point">Innenvisualisierung</li>
                                <li class="11 hide-me add-btn point">Außenvisualisierung</li>
                                <li class="12 hide-me add-btn point">Virtuelle Begehung</li>
                                <li class="13 hide-me add-btn point">Augmented Reality</li>
                            </ul>
                        </div>
                    </div>
                     <button type="button" class="btn btn-primary added-btn" title="Zur Übersicht." id="gesamt-weiter" >Zum Formular</button>



                 </div> 

Checkboxes:

<div class="">
      <ul class="checker-wrap">
      <li><input type="checkbox" id="0" value="Zielgruppendefinition" class="0 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="2" value="Exposé" class="2 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="3" value="Textdesign" class="3 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="4" value="Anzeigenkampagnen" class="4 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="5" value="Außenwerbung" class="5 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="6" value="Bautafelgestaltung" class="6 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="8" value="Website" class="8 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="9" value="Social Media" class="9 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="10" value="Digitale Präsentation" class="10 checker" name="checkbox-group[]"></li>
      <li><input type="checkbox" id="11" value="Visualisierungen" class="11 checker" name="checkbox-group[]"></li>
     <li><input type="checkbox" id="12" value="Virtuelle Begehung" class="12 checker" name="checkbox-group[]"></li>
     <li><input type="checkbox" id="13" value="Augmented Reality" class="13 checker" name="checkbox-group[]"></li>
    </ul>
    </div>

jQuery:

var $check = $(".checker-wrap");
$(".add-btn").click(function() {
        var $li = $(this).toggleClass("hide-me added haken");

        var li_class = $(this).attr('class').split(" ");
        var $wrap = $li.closest('.label-wrap');
        // trigger checkbox for form
        if (($li).hasClass("added")) {
            $check.find('.'+li_class[0]).addAttr('checked');
        } else {
            $check.find('.'+li_class[0]).removeAttr('checked');
        }

        $wrap.find('h1').toggleClass('top-added', $wrap.find('.added').length > 0);
        });

So the question is, what do I have to change, that the checkboxes get triggered if the li has the class added, and if not, the checkbox should be unchecked...




Aucun commentaire:

Enregistrer un commentaire