2

I have this code:

jQuery(document).ready(function($) { 
    var i = 0; 
    var values = []; 
    var element = $('.source');
    element.each(function(i) { 
        values[i++] = $(this).text();
    });
});

I want to assign each array value above, as the individual data-text value on another set of list elements. Something like this:

<ul id="list">
  <li data-text="arrayvalue1"></li>
  <li data-text="arrayvalue2"></li>
  <li data-text="arrayvalue3"></li>
</ul>

I don't understand how I would do this using jQuery.

6
  • $(this).attr('data-text') Commented Nov 28, 2016 at 19:42
  • $(this).data('text') Commented Nov 28, 2016 at 19:43
  • $(this)[0].dataset['text'] Commented Nov 28, 2016 at 19:44
  • .source holds the elements we are pulling from. The html already exists for adding the data-text attributes, I just don't know how to pull from one array and inject the data-text attributes to another Commented Nov 28, 2016 at 19:45
  • @RobMyrick something like this? Commented Nov 28, 2016 at 19:47

5 Answers 5

1

You can use attr() to assign data-text values with values from another array. With each() loop you are iterating over all li elements in ul and adding values from element array starting from 0, and you are also incrementing i by 1. So on second li, value of i will be 1 which is arrayvalue2 etc...

var element = ["arrayvalue1", "arrayvalue2", "arrayvalue3"]

var i = 0;
var values = $('ul li').each(function() {
  return $(this).attr('data-text', element[i++]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="list">
  <li></li>
  <li></li>
  <li></li>
</ul>

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

1 Comment

This worked: the element[i++] is what confused me for some reason. Going to except this answer
0

Instead of using jQuery's each(), try using this higher order function. I don't have a complete picture of how your object looks but something like this should work:

element.forEach((el) => {
    el.dataset.text = referencedValue
}

Comments

0

There's no need for the "i" var, just do a push to the array of values. Like this:

jQuery(document).ready(function($) { 
    var values = []; 
    var element = $('.source');
    element.each(function(i) {
        values.push($(this).data('text'));
    });
    console.log(values);
});

See this fiddle https://jsfiddle.net/masqueradecircus/errq8cp7/3/

Comments

0

Provided you have your source elements you can iterate through them and apply the data attribute referring to the index of the element:

var element = $('.source');
element.each(function(i) {
  $('#list > li').eq(i).attr('data-text', $(this).text());
  //or
  $('#list > li').eq(i).data('text', $(this).text());
});

Note: the difference of the two lines is:

.attr() will apply the value in the dom as an attribute to the element (visible in the html markup). The value can be retrieved with .data() or .attr()

The second one applies the value as an association to the element, which will not be reflected in the dom. This can only be retrieved with .data()


Example

Comments

0

Use $('.source li') and then you can use attr property in jquery to set the data-text attribute in another list. Please find the sample below: https://jsfiddle.net/vgade/km113ztL/

jQuery(document).ready(function($) { 
    var i = 0; 
    var values = []; 
    var element = $('.source li');
    element.each(function(i) { 
        $('#destination li')[i].attr("data-text",$(this).text());
        i++;
    });
});

2 Comments

What's the point of $($('#destination li')[i]) ? You are wrapping one jquery object on another
@empiric Yes you are right $('#destination li')[i] this will work too.

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.