1

I have a unordered list and each list element have "data-id" attribute, the idea is that using JQuery's each function iterate through each list element and get the "data-id" value and dynamically assign the position, ie to make a item slider that slides one item at a time. but on each the value of data I'm getting is "1", ie its taking value from the first element only, how do I fix it??

HTML

<div class="explore_matches">
   <p>Recent Matches</p>
   <ul class="clearfix item-slider">
       <li data-id="1" ><span>1</span></li>
       <li data-id="2" ><span>2</span></li>
       <li data-id="3" ><span>3</span></li>
       <li data-id="4" ><span>4</span></li>
       <li data-id="5" "><span>5</span></li>
       <li data-id="6" "><span>6</span></li>
       <li data-id="7" "><span>7</span></li>
       <li data-id="8" "><span>8</span></li>
    </ul>
    <button name="pre">Pre</button>
    <button name="next">Next</button>
</div>

JavaScript

var item_width = $('.item-slider').width();
var item_margin = (item_width-600)/5;
var items = $('ul.item-slider>li');

$.each(items,function(index){

    var pos = $('.item-slider > li').data('id') * 150;
    if(index === 0)     
        $(this).css("left", pos);
    else
        $(this).css("left", pos + index * item_margin);
    console.log(pos);
})
3
  • 3
    Where are you accessing the data-id attribute in your code? Commented Aug 18, 2016 at 5:23
  • oops, since that wasn't working i'm using the index of each function, wait let me edit, the question Commented Aug 18, 2016 at 5:28
  • Something wrong in your HTML. Remove extra quotes from 5th li. Commented Aug 18, 2016 at 6:51

3 Answers 3

9

Try this:

$('li').each(function() {
    var data = $(this).attr('data-id');   // it will give 1,2,3 and so on
    //do stuff to each
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks man, but please check the accepted answer, I think that is the right way to use data since there is a data function.
be careful with accepting simplistic answers @River CR Phoenix there is often a subtle reason why one option is better than another.
4

You can get the elements attribute using getAttribute method

var item_width = $('.item-slider').width();
var item_margin = (item_width-600)/5;
var items = $('ul.item-slider>li');

$.each(items,function(index, node){
    var pos = index * 150;
    alert(node.getAttribute('data-id'));
    if(index === 0)     
        $(this).css("left", pos);
    else
        $(this).css("left", pos + index * item_margin);
    console.log(pos);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="explore_matches">
   <p>Recent Matches</p>
   <ul class="clearfix item-slider">
       <li data-id="1" ><span>1</span></li>
       <li data-id="2" ><span>2</span></li>
       <li data-id="3" ><span>3</span></li>
       <li data-id="4" ><span>4</span></li>
       <li data-id="5" "><span>5</span></li>
       <li data-id="6" "><span>6</span></li>
       <li data-id="7" "><span>7</span></li>
       <li data-id="8" "><span>8</span></li>
    </ul>
    <button name="pre">Pre</button>
    <button name="next">Next</button>
</div>

1 Comment

yeah, this works, but is there any way to get the data value from each li??
3

Just replace

var pos = $('.item-slider > li').data('id') * 150; 

with

var pos = $(this).data('id') * 150;

7 Comments

Awesome, such simple thing hard to notice, thanks man Nikolay,
take care with this - I believe that using the .data() method - the value is cached and may cause altered functionalty - I would suggest .attr('data-id'); as @Mayank Pandey has suggested
@gavgrif - What makes you think it is cached?
i used to use .data('x') a lot but was reading in SO about the different aspects of data attributes and people smarter than me pointed it put - so now I use .attr('data-x'); as the preferred method of getting / seeting the data attribute value.
Well, it seems the OP is safe enough if he doesn't start changing the attribute value afterwards, which I'm quite sure of since it is id-something. Otherwise he may switch to var pos = $(this).attr('data-id') * 150;.
|

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.