0

Just wondering if anyone can help. I currently have code like this:

<section>
  <span class="tags"></span>

  <label for="shoes">Shoes</label>
  <input type="checkbox" id="shoes">

  <label for="jeans">Jeans</label>
  <input type="checkbox" id="jeans">

  <label for="tops">Tops</label>
  <input type="checkbox" id="tops">
</section>

<section>
  <span class="tags"></span>

  <label for="monkey">monkey</label>
  <input type="checkbox" id="monkey">

  <label for="lion">lion</label>
  <input type="checkbox" id="lion">

  <label for="dog">dog</label>
  <input type="checkbox" id="dog">
</section>

Each 'section' is dynamically produced. How do I go about inserting the value of each input into the span of each section when checked. I have been playing around with Arrays but stumbling due to each section being produced dynamically.

Can any of you help me out?

2
  • 1
    Have you tried getting it working for a single section? What's your code for that? Commented Jun 17, 2015 at 8:52
  • Can i have your javascript array. Commented Jun 17, 2015 at 9:06

2 Answers 2

1

Better give each checkbox input a value, anyway, I'll use id instead.

// Listen to all checkboxes
$('section input[type="checkbox"]').click(function(e) {
    var $this = $(e.target);
    // Find the container of same group.
    var $parent = $this.parent('section');
    // Find all checked ones.
    var checked = $parent.find('input[type="checkbox"]:checked');
    // Map the value or id, to an array.
    var result = $.map(checked, function(ele) {
        return $(ele).attr('id');
    });
    // Get the result, use it whatever you want.
    $parent.find('.tags').text(result.join(' '));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
  <span class="tags"></span>

  <label for="shoes">Shoes</label>
  <input type="checkbox" id="shoes">

  <label for="jeans">Jeans</label>
  <input type="checkbox" id="jeans">

  <label for="tops">Tops</label>
  <input type="checkbox" id="tops">
</section>

<section>
  <span class="tags"></span>

  <label for="monkey">monkey</label>
  <input type="checkbox" id="monkey">

  <label for="lion">lion</label>
  <input type="checkbox" id="lion">

  <label for="dog">dog</label>
  <input type="checkbox" id="dog">
</section>

Sign up to request clarification or add additional context in comments.

1 Comment

This is absolutely perfect. Such clean code too. Love it. Thank you fuyushimoya.
1

Use this javascript:

<script type="text/javascript">
window.addEventListener("load",function(){
    var sections = document.getElementsByTagName("section");
    for(var i=0; i<sections.length; i++){
        var n = 0;
        sections[i].span = sections[i].getElementsByTagName("span")[0];
        sections[i].checkboxes = [];
        var inputs = sections[i].getElementsByTagName("input");
        for(var c=0; c<inputs.length; c++){
            if(inputs[c].type!="checkbox"){continue}
            sections[i].checkboxes[n++]=inputs[c];
            inputs[c].onchange=function(){this.parentNode.getValues();}
        }
        sections[i].getValues = function(){
            var o=[], n=0;
            for(var i=0; i<this.checkboxes.length; i++){if(this.checkboxes[i].checked){o[n++] = this.checkboxes[i].id;}}
            this.span.innerHTML = o.join(", ");
        };
    }
},false);
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.