1

I have a small sample of HTML that looks like this

<ul>
    <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
    <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
    <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
    <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
    <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
</ul>

I can't edit the HTML itself due to what I'm doing, but I need to basically add a number each time to data-side-to using only js, like so

<ul>
    <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
    <li data-slide-to="1" data-target="#myCarousel" class="appendLi"></li>
    <li data-slide-to="2" data-target="#myCarousel" class="appendLi"></li>
    <li data-slide-to="3" data-target="#myCarousel" class="appendLi"></li>
    <li data-slide-to="4" data-target="#myCarousel" class="appendLi"></li>
</ul>

Any ideas?

6 Answers 6

1

Try this simple code,

$(document).ready(function(){
  $("ul>li").each(function(i,j){
     $(this).attr("data-slide-to",i);
  });
  console.log($("ul").html());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
</ul>

I have taken index of li and assigned it to data-slide-to attribute of every li.

I hope this will help.

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

3 Comments

Thanks, it worked mate. I was making an instagram carousel, in case you were wondering.
:D I wondered specific rather than wander in what's going on :) You're welcome mate!
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
0

Try this one

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

  $('ul li').each(function(i,el)
{
   $(el).attr('data-slide-to',i); // This is your rel value
});

});
</script>

Comments

0

Try like this..

Core JS

var lis = document.querySelectorAll('li');
lis.forEach((element,index)=>{
element.setAttribute('data-slide-to',index);
});
console.log(lis);

Example:

var lis = document.querySelectorAll('li');
lis.forEach((element,index)=>{
element.setAttribute('data-slide-to',index);
console.log(element);
});
<ul>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
</ul>

Comments

0

Use each to iterate between li and change the value of data-slide-to with its index

$(document).ready(function(){
  $("li").each(function(index){
    $(this).attr("data-slide-to", index)
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
<li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
</ul>

1 Comment

yes, you are right silly mistake. I have updated the code.
0

You could use jQuery method each() to loop through all the li's and edit the data-* attributes with the index returned from the each() method using jQuery data() method :

$('ul>li').each(function(index, value){
    $(this).data('slide-to', index);
})

Hope this helps.

$('ul>li').each(function(index, value){
    $(this).data('slide-to', index);
})

//Debug
$('ul>li').each(function(i,v){
    console.log( $(this).data('slide-to') );
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
</ul>

Comments

0

Try this code, hope this helps you

$('ul>li').each(function(i, obj){
  $(this).attr('data-slide-to',i); 
})

  console.log($("ul").html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
  <li data-slide-to="0" data-target="#myCarousel" class="appendLi"></li>
</ul>

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.