1

I am after some assistance in passing the value of my span classes to a data attribute (data-groups) contained in their parent divs all with the same class (.item). Here is my current code.

<div class="item" href="#" data-groups="">
   <div class="caption">
      <p><span class="value1"></span>Description</p>
    </div>
</div>

<div class="item" href="#" data-groups="">
   <div class="caption">
      <p><span class="another-value"></span>Description</p>
    </div>
</div>

<div class="item" href="#" data-groups="">
   <div class="caption">
      <p><span class="third-value"></span>Description</p>
    </div>
</div>

<script type="text/javascript">
$(".item").attr("data-groups", function() {
  return $('.caption p span').attr('class');
});
</script>

This works somewhat but it populates all data-groups attributes with "value1". I am wanting them all to receive the data attribute from their respective child span classes. Eg, First item div should have a 'data-groups'attribute of 'value1', second with 'another-value', etc.

I'm a little rusty with jquery but learning as I go. Any assistance is appreciated.

1
  • 1
    try ` $(this).find('.caption p span').attr('class')` Commented Dec 19, 2017 at 9:24

1 Answer 1

1

Try this:

$(".item").attr("data-groups", function() {
  return $(this).find('.caption p span').attr('class');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I knew it was something simple

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.