0

I am new to programming and stack overflow, so please forgive me if I am not formatting my question properly :)

I'm trying to loop through an array containing three spans, and log the 'data-strength' attribute value. I am able to get the individual spans with $el[i], and am also able to get the attribute value with $el.attr('data-strength'). When I combine the two however $el[i].attr('data-strength'), an error is returned. I assume this is a syntax error, but I'd love some guidance.

I've also included the pen link below. Thanks!!!

<div id="container">
  <button id="trigger">Click Me</button>

  <div id="strength">
    <span data-strength="60"></span>
    <span data-strength="50"></span>
    <span data-strength="40"></span>
  </div>
</div>

var $trigger = $("#trigger"),
    $el = $("#strength span"),
    $counter = $el.length;

$trigger.click(function(){
    for(var i = 0; i < $counter; i++){   
        var $result = $el[i].attr('data-strength');
    console.log($result);
    }  
});

https://codepen.io/joeylane/pen/dmGpdq

2 Answers 2

1

$el[i] is not a jQuery object. You have to $($el[i]) to use attr

var $trigger = $("#trigger"),
  $el = $("#strength span"),
  $counter = $el.length;

$trigger.click(function() {
  for (var i = 0; i < $counter; i++) {
    var $result = $($el[i]).attr('data-strength');
    console.log($result);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <button id="trigger">Click Me</button>

  <div id="strength">
    <span data-strength="60"></span>
    <span data-strength="50"></span>
    <span data-strength="40"></span>
  </div>
</div>


You can also use each to loop thru your array and use this as selector.

$trigger.click(function(){
    $el.each(function(){
        var $result = $(this).attr('data-strength');
        console.log($result);
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Then there's $().data, which automatically parses JSON and lets you store arbitrary data with the element (though it won't back-edit the attribute)
You can also use .eq() to get a jquery object of the element at that index
Thanks guys. All your suggestions are great and correct :)
0

Just iterate ver the spans within the div and use $(this).() to get the data attribue of each.

$("#trigger").click(function(){
  $("#strength span").each(function(){
    var strength = $(this).attr('data-strength');
    console.log(strength);
  })  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <button id="trigger">Click Me</button>

  <div id="strength">
    <span data-strength="60"></span>
    <span data-strength="50"></span>
    <span data-strength="40"></span>
  </div>
</div>

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.