3

I'm using the Dragsort plugin and we want to tag IDs in an array and print them.

My Html Code Is:

<ul id="list1">
    <li class="ss"><div>1</div></li>
    <li><div>2</div></li>
    <li><div>3</div></li>
    <li><div>4</div></li>
    <li><div>5</div></li>
    <li><div>6</div></li>
    <li><div>7</div></li>
    <li><div>8</div></li>
    <li><div>9</div></li>
</ul>
<p id="demo"></p>

and my jQyery codes is

$("#list1, #list2").dragsort({ dragSelector: "div", dragBetween: true, dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });

function saveOrder() {
    var data = $("#list1 li").map(function() { return $(this).children().html(); }).get();
    $("input[name=list1SortOrder]").val(data.join("|"));
};

$( "#list1 li" ).each(function( index ) {
    let items = [];
    items = [ index + 1];
    console.log(items.map(() => index));
    $(this).attr("id",items);
});

$('#hamid').click(function () {
    $( "#list1 li" ).each(function() {
        its = [ $(this).attr('id')];
    });
});

When I want to return id <li> return last of them

5
  • The question is not clear. What is the problem you are facing? Commented Nov 14, 2018 at 10:09
  • problem is when i want to return all ids in "its" variable , it return just last id Commented Nov 14, 2018 at 10:11
  • You're reassigning its with a new array when iterating through the whole collection of #list1 li. Just use .map() to return the array of IDs in all the <li> elements in the click handler. Commented Nov 14, 2018 at 10:13
  • can you give me code it ? Commented Nov 14, 2018 at 10:15
  • @arman Sure. See my answer: there are two ways to do it: a basic way, and a truly jQuery way :) Commented Nov 14, 2018 at 10:19

2 Answers 2

3

You may want to change the line

its = [ $(this).attr('id')];
//this will only create an instance of an array with one element.

change the last function to something like:

$('#hamid').click(function () {
    var ids = [];
    $( "#list1 li" ).each(function() {
        ids.push( $(this).attr('id') );
    });
    console.log(ids);  
    // this will print an array with all ids inside
    // then you can return it or do whatever you want with it
});
Sign up to request clarification or add additional context in comments.

Comments

1

I guess your issue is that you want to get all the IDs of the elements that matches #list1 li, as seen in the last 5 lines of your code. The reason why it is not working is because you are reassigning a brand new array to the its variable. I suggest doing one of the two:

  • Solution 1: Push into the array instead of reassigning it
  • Solution 2: Use .map() + .get() to return an array of all IDs

Solution 1 (basic)

Use Array.prototype.push() to append IDs to a pre-existing array. Remember that instead of converting this into a jQuery object and then accessing its ID via the .attr() method (by doing $(this).attr('id')), you can easily do this.id:

$('#hamid').click(function () {
    var its = [];
    $('#list1 li').each(function() {
        its.push(this.id);
    });
    console.log(its);
});

Solution 2 (better)

Use a combination of .map() and .get(). This is the most jQuery-way to do it:

$('#hamid').click(function () {
    var its = $('#list1 li').map(function() {
        return this.id;
    }).get();
    console.log(its);
});

The reason why .get() is needed is because .map() returns a jQuery collection instead of an actual array. To retrieve the array itself, you will need to append .get() after .map().

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.