2

I have a html structure that looks like the following:

  <div>
    <div questionName="${data.Name}" class="item">
            <div class="left"><input type="checkbox" class="include-checkbox"/></div>
            <div class="center">{data.Name}</div>
            <div class="right"><input class="comment" placeholder="Comment (optional)"/>
    </div>
   ...
</div>

With multiple "items" (divs with class="item"). These are generated. Using jQuery, I need to go through those divs, and if the checkbox for that item is checked, add the value from the center div and input value from the right div as a key-value pair to an object (or a JSON string). So my resulting object should look like this:

{
 "an item" : "some comment the user typed in",
 "another item": "some other comment",
 "yet another item" : ""
}

the comment is optional, and if the user doesn't fill anything in it should just contain empty string.

I am able to get all the items, because they have the same class, and run a foreach on them:

function submitItems() {
    let items = {};
    $('.item').each(function() {
        ?   
    });
}

But how do I then access the elements within the item? For example I want to check the status of the checkbox, but when I do $('.include-checkbox') it will give me all of them, not just the one that's inside my element. Also the function within the .each() construct, seems to have its own scope so the items object is not visible.

I have control over how the generated html looks, so I can add classes, ids or other attributes if need be.

9
  • "But how do I then access the elements within the item": jQuery has .children() method Commented Dec 5, 2019 at 14:31
  • In the .each() callback the "item" <div> elements will be referred to by this, so you can use $(this).find(".center") for example. Commented Dec 5, 2019 at 14:33
  • .find() documentation Commented Dec 5, 2019 at 14:33
  • @Pointy in my opinion find is too heavy, if you need direct children. Else it is good. Commented Dec 5, 2019 at 14:34
  • 1
    @Andris It's going to be implemented by a .querySelectorAll() in most cases, so it'll be perfectly fine. I don't know where people get browser performance ideas from but modern browsers on computers less than 10 years old are extremely fast. Commented Dec 5, 2019 at 14:37

2 Answers 2

3

You can use find() method in jQuery.

function submitItems() {
    let items = {};
    $('.item').each(function() {
       // check checkbox is checked 
       if($(this).find('.left :checkbox')[0].checked)
          // if checked then get text and input value and set the property
          items[$(this).find('.center').text().trim()] = $(this).find('.right input').val();
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0
function submitItems() {
    var items = {};
    $('.item').each(function (i, e) {

        var name = $("div", e).slice(1).text();
        var comment = $(".comment", $("div", e).slice(2)).first().val();
        var item = { name: comment };
        items[name] = comment;

    });
    var jsonstring=JSON.stringify(items);
    //do something
}

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.