0

I am working on a project in which I am using dragster library.I am trying to build a cards game . I stuck when I am trying to get values of card using dragster function.It runs loop twice which I dont want(first it runs 13 times then it run to what I want) .I want only that loop that runs last as it contain all elements which I need.First one run 13 times always.Its running like this:

0
1
.
.
upto 12
then it again run
0 to 11(this loop I want).

How can I ignore above loop. My code:

$('.test .block').find('div.Hearts').each(function(i,val){
                 let h = $(val).find('p#number').text();
                  console.log(h)

            })
3
  • 2
    You need to find out why it runs twice, rather than add more code to stop it doing that. Commented Dec 20, 2017 at 11:42
  • you say the second loop has what you want ... so what does the first loop (0 - 12) "have"? and this code snippet suggests you have multiple <p> elements with the same id - which is invalid HTML for a start - also, there are 13 hearts in a deck of cards, just sayin' Commented Dec 20, 2017 at 11:44
  • I am using dragster package and using dragster function also which also on drag it runs loops and after dropping element it also run (this one which I want) Commented Dec 20, 2017 at 11:48

1 Answer 1

1

This looks like an issue with understanding jQuery's

Each http://api.jquery.com/jquery.each/

Find https://api.jquery.com/find/

The issue most likely relates to you having multiple elements returned in the find which the each then runs on.

I would advise you either use more specific selectors - for debugging log out $(this) from inside the each to see which element it's targeting.

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

1 Comment

This is what I was thinking, there must be multiple elements returned by find() method.

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.